2012-08-27 105 views
2

我知道這是一個已經回答了幾千次的基本問題,但我無法完成它的工作。VB:如何將DataTable綁定到DataGridView?

我在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,我嘗試顯示數據表tableTempJDLDataGridViewBerechnung

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

但我不知道如何填寫在DataGridView ...

+0

[如何:將數據綁定到Windows窗體DataGridView控件(HTTP以下列方式的BindingSource的://msdn.microsoft.com/en-us/library/fbk67b6z.aspx) –

+0

@TimSchmelter我問這個問題之前,我讀了這個,但我不能讓它工作。另一個很好的例子是在MSDN上:[如何:將數據綁定到Windows窗體DataGridView控件](http://msdn.microsoft.com/en-us/library/fbk67b6z(v = vs.100).aspx)。但是這個使用外部數據庫並需要使用連接字符串。 – Nicolas

回答

3

簡單地說,你可以讓你的表作爲數據源

Me.bindingSourceBerechnung .DataSource = tableTempJDL 

之後,你可以綁定在上面下面的方式在你的datagridview綁定源:

Me.DataGridViewBerechnung.DataSource = Me.bindingSourceBerechnung 
+0

它的工作,太棒了!我只需要在Main.vb中的Sub外聲明tableTempJDL。然後,下面一行工作:'Me.bindingSourceBerechnung.DataSource = Main.tableTempJDL'。非常感謝LolCoder! – Nicolas

相關問題