[英]c# access and update datagridview data from another user control
[英]Update datagridview with data from another form (C#)
我在尝试找出如何在C#中更新datagridview时遇到问题。 我有两种形式(Form1:使用datagridview / Form2:使用文本框和“保存”按钮。)
我使用datagridview上的doubleclick函数打开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中的地址?
已经感谢您的回答。
如果从数据库表填充了datagridview,那么您必须更新该表并将其绑定到datagridview一次,再单击“保存”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.