vb.net VB:如何将 DataTable 绑定到 DataGridView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12141519/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:15:46  来源:igfitidea点击:

VB: How to bind a DataTable to a DataGridView?

vb.netvisual-studio-2010datagridviewdatatable

提问by Nicolas

I know this is a basic question that has already been answered thousand times, but I can't make it work.

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

I am working in Visual Studio 2010 and have two forms in my Windows Application. In the first one (Main.vb), the user enters his inputs and the calculation takes place. In the second one (DataAnalysis.vb) the calculation results are displayed.

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

In Main.vb, I create the temp table that will contains all the intermediary calculation steps:

在 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

Then, in DataAnalysis.vb, I try to display the DataTable tableTempJDLinto the DataGridViewBerechnung:

然后,在 DataAnalysis.vb 中,我尝试将 DataTable 显示tableTempJDLDataGridViewBerechnung

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

But then I don't understand how to fill the DataGridView...

但是后来我不明白如何填充DataGridView ...

采纳答案by Akash KC

Simply, you can make your table as the datasource of bindingsource in following way:

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

 Me.bindingSourceBerechnung .DataSource = tableTempJDL

Later on, you can bind above binding source in your datagridview in following way:

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

 Me.DataGridViewBerechnung.DataSource = Me.bindingSourceBerechnung