1
votes

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.

1

1 Answers

1
votes

If the datagridview gets populated from a database table then you must update the table and bind it to the datagridview one more time when you click save.