绑定datagriview
Uri uri = new Uri("http://localhost:49423");//地址
HttpClient clien = new HttpClient();
clien.BaseAddress = uri;
clien.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage message = null;
if(string.IsNullOrWhiteSpace(name))
message= clien.GetAsync("api/show/Get").Result;
else
message = clien.GetAsync("api/show/Getcha?id="+name).Result;
if (message.IsSuccessStatusCode)
{
var data = message.Content.ReadAsStringAsync().Result;
var data2 = JsonConvert.DeserializeObject<DataTable>(data);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.DataSource = data2;
修改
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
update up = new update();
up.names.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
up.id.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
up.zhiwu.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
up.bili.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
up.phone.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
up.dizhi.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
up.beizhu.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
up.ShowDialog();//showdialog修改之后还能执行下面的代码
bind("");
}
保存修改
private void button1_Click(object sender, EventArgs e)
{
yuan mm = new yuan()
{
Beizhu = beizhu.Text,
Bili = Convert.ToDecimal(bili.Text),
Dizhi = dizhi.Text,
Names = names.Text,
Phone = phone.Text,
Zhiwu = zhiwu.Text,
id= Convert.ToInt32( id.Text)
};
Uri uri = new Uri("http://localhost:49423");//地址
HttpClient clien = new HttpClient();
clien.BaseAddress = uri;
clien.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpContent cotent = new StringContent(JsonConvert.SerializeObject(mm));
cotent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage message = clien.PutAsync("api/show/update", cotent).Result;
if (message.IsSuccessStatusCode)
{
var data = message.Content.ReadAsStringAsync().Result;
if (Convert.ToInt32(data) > 0)
{
MessageBox.Show("修改成功");
this.Close();
}
}
}