我在尝试了解如何在C#中更新datagridview时遇到问题。 我有两种形式(Form1:使用datagridview / Form2:使用文本框和"保存"按钮。)
我在datagridview上使用doubleclick-function打开Form2,并显示所选行的详细信息。
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentRow != null)
{
WCustomer row = dataGridView1.CurrentRow.DataBoundItem as WCustomer;
CustomerDetail c1 = new CustomerDetail(_Proxy, row.CustomerID);
c1.CompanyName = dataGridView1.CurrentRow.Cells[1].Value.ToString();
c1.ContactName = dataGridView1.CurrentRow.Cells[2].Value.ToString();
c1.ContactTitle = dataGridView1.CurrentRow.Cells[3].Value.ToString();
c1.Address = dataGridView1.CurrentRow.Cells[4].Value.ToString();
c1.City = dataGridView1.CurrentRow.Cells[5].Value.ToString();
c1.Region = dataGridView1.CurrentRow.Cells[6].Value.ToString();
c1.PostalCode = dataGridView1.CurrentRow.Cells[7].Value.ToString();
c1.Country = dataGridView1.CurrentRow.Cells[8].Value.ToString();
c1.Phone = dataGridView1.CurrentRow.Cells[9].Value.ToString();
c1.Fax = dataGridView1.CurrentRow.Cells[10].Value.ToString();
c1.passDgvValueToCustomerDetail();
c1.Show();
}
else
{
MessageBox.Show("No selected row!");
}
}
要将gridview中的数据导入到我的第二个表单的文本框中,我使用了以下代码:
public void passDgvValueToCustomerDetail()
{
txtCompanyName.Text = CompanyName;
txtContactName.Text = ContactName;
txtContactTitle.Text = ContactTitle;
txtAddress.Text = Address;
txtCity.Text = City;
txtRegion.Text = Region;
txtPostalCode.Text = PostalCode;
txtCountry.Text = Country;
txtPhone.Text = Phone;
txtFax.Text = Fax;
}
我现在如何更新例如datagridview中的地址,方法是在点击" Save"之后将其替换为我在第二个表单中更改的地址。 ?
非常感谢您的回答。
答案 0 :(得分:1)
如果从数据库表中填充了datagridview,则必须更新表并在单击“保存”时再将其绑定到datagridview。