C# 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:
VB.NET Code:
Dim dt As New DataTable
dt.Columns.Add("ID", GetType(Integer)).AutoIncrement = True
dt.Columns.Add("Name", GetType(String))
dt.Rows.Add(Nothing, "Jane")
dt.Rows.Add(Nothing, "John")
dt.Rows.Add(Nothing, "Jack")
Me.BindingSource1.DataSource = dt
Me.DataGridView1.DataSource = Me.BindingSource1
Now run the project and observe the data you added to the DataTable displayed in the bound grid via the BindingSource.
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 and type "public sub new" and press Enter. That will generate a default constructor like so:
VB.NET Code:
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
10. Add a class level variable like so:11. Change the constructor from the default above to the following:
VB.NET Code:
Public Sub New(ByVal data As DataRow)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.data = data
Me.idText.Text = data("ID").ToString()
Me.nameText.Text = CStr(data("Name"))
End Sub
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.
12. Go back to the first form's code window.
13. Select DataGridView1 from the drop-down list at the top-left and CellDoubleClick from the top-right. That will create an empty handler for the grid's CellDoubleClick event.
14. Add the following code to the empty event handler:
VB.NET Code:
Dim row As DataRow = DirectCast(Me.BindingSource1.Current, DataRowView).Row
Using dialogue As New Form2(row)
dialogue.ShowDialog()
End Using
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.
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:
VB.NET Code:
Me.data("Name") = Me.nameText.Text
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.