Reputation: 2583
I have a datagridview on a windows form. I have a bindingsource and datatable which are created at runtime which I intend to use to bind and keep my datagridview updated.
When debugging I see that my datatable is being populated with rows. I can also see my bindingsource's datasource has data when you open the visualizer.
My issue is that my datagridview stays blank and never seems to get any of the data that my datatable and bindingsource are getting.
Code Sample:
Private bs1 As New BindingSource
Private TraysToScanDatatable As New DataTable
in my constructor
TraysToScanDatatable.Columns.Add(New DataColumn("time", GetType(DateTime)))
TraysToScanDatatable.Columns.Add(New DataColumn("scanner", GetType(Integer)))
TraysToScanDatatable.Columns.Add(New DataColumn("traynumber", GetType(Integer)))
bs1.DataSource = TraysToScanDatatable
UpdateDataGridView(TraysToReadDataGridView, bs1.DataSource) 'if I do not set my datagridview with a delegate here then I cannot update the binding source in the timer.
update timer logic
TraysToScanDatatable.Rows.Add(New Object() {DateTime.Now, 1, lastScanner1TrayReceived})
Me.bs1.DataSource = TraysToScanDatatable
me.bs1.MoveLast
and my updatedatagridview routine
Public Sub UpdateDataGridView(ByVal control As DataGridView, ByVal newdata As DataTable)
If Not control.InvokeRequired Then
control.DataSource = newdata
Else
Invoke(New Action(Of DataGridView, DataTable)(AddressOf UpdateDataGridView), control, newdata)
End If
End Sub
Upvotes: 0
Views: 12510
Reputation: 2583
The solution to this issue was multi-part.
I had to assign datagridview's datasource to bindingsource object (instead of bindingsource.datasource) as Carlos Landeras pointed out.
In addition to this I had to call:
bindingsource.ResetBindings(False)
DataGridView.Refresh()
Upvotes: 0
Reputation: 11063
You have to assign the BindingSource
object itself to the datagridView.Datasource
and not the BindingSource.Datasource
.
This line of yours:
If Not control.InvokeRequired Then
control.DataSource = newdata
Else
is assigning bs1.DataSource
to grid Datasource instead of the BindingSource object
Try doing it like this:
datagridview.DataSource = bs1
bs1.Datasource = TraysToScanDatatable
And if this works, then apply your logic following steps.
Upvotes: 2