vb.net 数据表通过 bindingsource 绑定到 datagridview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19503808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
datatable bound to datagridview through bindingsource
提问by TWood
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.
我在 Windows 窗体上有一个 datagridview。我有一个在运行时创建的 bindingsource 和数据表,我打算用它们来绑定和保持我的 datagridview 更新。
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.
调试时,我看到我的数据表正在填充行。当您打开可视化工具时,我还可以看到我的 bindingsource 的数据源有数据。
My issue is that my datagridview stays blank and never seems to get any of the data that my datatable and bindingsource are getting.
我的问题是我的 datagridview 保持空白,似乎从未获得我的数据表和 bindingsource 获得的任何数据。
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
和我的 updatedatagridview 例程
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
回答by Carlos Landeras
You have to assign the BindingSourceobject itself to the datagridView.Datasourceand not the BindingSource.Datasource.
您必须将BindingSource对象本身分配给datagridView.Datasource而不是BindingSource.Datasource。
This line of yours:
你的这一行:
If Not control.InvokeRequired Then
control.DataSource = newdata
Else
is assigning bs1.DataSourceto grid Datasource instead of the BindingSource object
正在分配bs1.DataSource给网格数据源而不是BindingSource object
Try doing it like this:
尝试这样做:
datagridview.DataSource = bs1
bs1.Datasource = TraysToScanDatatable
And if this works, then apply your logic following steps.
如果这有效,请按照以下步骤应用您的逻辑。
回答by TWood
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.
正如 Carlos Landeras 指出的那样,我必须将 datagridview 的数据源分配给 bindingsource 对象(而不是 bindingsource.datasource)。
In addition to this I had to call:
除此之外,我不得不打电话:
bindingsource.ResetBindings(False)
DataGridView.Refresh()

