VB version here.
A lot of people ask how to update a record from a bound grid using a dialogue. The process is so easy it's laughable, but most people don't realise because they don't understand how powerful data-binding is. Following are instructions on how to update a row from a DataGridView bound to a DataTable using a dialogue. I've also attached a working sample but I strongly suggest that you follow the instructions and create your own project to get a better feel for it.
1. Create a new Windows Forms project.
2. Add a DataGridView and a BindingSource to the form.
3. Double-click the title bar of the form to create a Load event handler and add the following code:
Now run the project and observe the data you added to the DataTable displayed in the bound grid via the BindingSource.
DataTable dt = new
this.bindingSource1.DataSource = dt;
this.dataGridView1.DataSource = this.bindingSource1;
4. Add a new form to the project.
5. Add a Label to the form and set its Text property to "ID:".
6. Add a TextBox to the form, set its Name property to "idText" and its ReadOnly property to True.
7. Add a Label to the form and set its Text property to "Name:".
8. Add a TextBox to the form and set its Name property to "nameText".
9. Press F7 to open the code window.
10. Add a class level variable like so:11. Change the constructor from the default to the following:
That means that to create an instance of this form you need to supply a DataRow, the ID and Name fields of which will be displayed in the TextBoxes.
public Form2(DataRow data)
this.data = data;
this.idText.Text = data["ID"].ToString();
this.nameText.Text = (string)data["Name"];
12. Go back to the first form's design window.
13. Select dataGridView1, open the Properties window, click the Events button and double-click the CellDoubleClick event. That will create an empty handler for the grid's CellDoubleClick event.
14. Add the following code to the empty event handler:
Now run the project and double-click any cell in the grid and observe the data for that record displayed in a dialogue. Note that you can click the column headers to sort the data and double-clicking will still always display the correct data. That's one of the advantages of using a BindingSource.
DataRow row = ((DataRowView)this.bindingSource1.Current).Row;
Form2 dialogue = new
15. Go back to the design window for Form2.
16. Add a Button, set its Name property to "okButton", its Text property to "OK" and its DialogResult property to OK.
17. Add a Button, set its Name property to "cancelOperationButton" and its Text property to "Cancel".
18. Select the okButton for the form's AcceptButton property.
19. Select the cancelOperationButton for the form's CancelButton property.
20. Double-click the okButton to create an empty Click event handler.
21. Add the following code to the empty event handler:
That's it. You've finished the whole thing. Now you can run the project and double click a record to open it for editing in the dialogue. You can change the value of the Name property and press the OK button and observe the grid update automatically. Note also that if you edit the name field in the dialogue and press Cancel no changes are made to the grid. Finally, note that you can also add new rows and then edit them the same way.
this.data["Name"] = this.nameText.Text;