用另一种形式(c#)的数据更新datagridview



我在试图找出如何在c#中更新datagridview时遇到了问题。我有两个表单(Form1:带datagridview/Form2:带文本框和"保存"按钮)

我在数据视图上使用双击功能打开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从数据库表中填充,那么您必须更新表并在单击save时再次将其绑定到datagridview

最新更新