简体   繁体   中英

Update datagridview with data from another form (C#)

I'm having issues trying to find out how to update a datagridview in C#. I have two forms (Form1: With the datagridview / Form2: With textboxes and a "Save" button.)

I open Form2 with a doubleclick-function on the datagridview and show the details from the selected Row.

    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!");
        }
    }

To import the data from the gridview into the textboxes of my 2nd form, i used this code:

    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;
    }

How do I now update for example the address in the datagridview by replacing it with the address i changed in my 2nd Form after clicking "Save" ?

Thanks already for your answers.

如果从数据库表填充了datagridview,那么您必须更新该表并将其绑定到datagridview一次,再单击“保存”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM