【问题标题】:VB: How to bind a DataTable to a DataGridView?VB:如何将 DataTable 绑定到 DataGridView?
【发布时间】:2012-08-27 11:56:32
【问题描述】:

我知道这是一个已经被回答了一千次的基本问题,但我无法让它发挥作用。

我在 Visual Studio 2010 中工作,并且在我的 Windows 应用程序中有两个表单。在第一个(Main.vb)中,用户输入他的输入并进行计算。在第二个(DataAnalysis.vb)中显示计算结果。

在 Main.vb 中,我创建了包含所有中间计算步骤的临时表:

Dim tableTempJDL As DataTable = New DataTable("TempJDL")
Dim column As DataColumn

column = New DataColumn("ID", GetType(System.Int32))
tableTempJDL.Columns.Add(column)

column = New DataColumn("PthObjekt", GetType(System.Double))
tableTempJDL.Columns.Add(column)

'further columns are after created using the same method

然后,在 DataAnalysis.vb 中,我尝试将 DataTable tableTempJDL 显示到 DataGridViewBerechnung 中:

Public bindingSourceBerechnung As New BindingSource()
Me.DataGridViewBerechnung.DataSource = Me.bindingSourceBerechnung

然后我不明白如何填充 DataGridView...

【问题讨论】:

标签: vb.net visual-studio-2010 datagridview datatable


【解决方案1】:

简单地说,您可以通过以下方式将您的表作为 bindingsource 的数据源:

 Me.bindingSourceBerechnung .DataSource = tableTempJDL

稍后,您可以通过以下方式在 datagridview 中绑定上述绑定源:

 Me.DataGridViewBerechnung.DataSource = Me.bindingSourceBerechnung 

【讨论】:

  • 成功了,太棒了!我只需要在 Main.vb 的 Sub 之外声明 tableTempJDL。然后以下行起作用:Me.bindingSourceBerechnung.DataSource = Main.tableTempJDL。非常感谢 LolCoder!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 2015-11-29
  • 1970-01-01
  • 2011-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多