The
DataGridView
gets updated when the
DataSource
is modified when it is bound using the
BindingSource
. I have made a small code sample as below. To run the sample, create a Windows Forms application, double click on the Form1 in Design view to open the code file, replace the entire contents of the code file
Form1.vb
with the following code and run the application.
Public Class Form1
Dim DataGridView1 As New DataGridView()
Dim ButtonUpdate As New Button()
Dim BindingSource1 As New BindingSource()
Dim DataTable1 As New DataTable()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataTable1.Columns.Add("Column1", GetType(System.String), Nothing)
With DataTable1.Rows
.Add("Row1")
.Add("Row2")
.Add("Row3")
End With
BindingSource1.DataSource = DataTable1
DataGridView1.DataSource = BindingSource1
ButtonUpdate.Dock = DockStyle.Bottom
ButtonUpdate.Text = "Update"
AddHandler ButtonUpdate.Click, AddressOf ButtonUpdate_Click
Me.Controls.Add(ButtonUpdate)
DataGridView1.Dock = DockStyle.Fill
Me.Controls.Add(DataGridView1)
End Sub
Private Sub ButtonUpdate_Click(ByVal s As Object, ByVal e As EventArgs)
DataTable1.Rows(0)(0) = "Row1 modified"
DataTable1.Rows.Add("Row4")
End Sub
End Class
In case the
DataGridView
does not get updated then the
ResetBindings
method of
BindingSource
which is explained here
BindingSource.ResetBindings Method[
^] can be used.
I think this Code Project article
A Detailed Data Binding Tutorial[
^]
may be helpful to understand the
DataBinding
concept.