2010-06-15 94 views
0

我有一個DataGridView正在填充表中的數據。在這個表格中有一個名爲'group'的列,它的另一個表中有一個單獨的組的ID。'交叉引用'DataTable的

我想要做的是在DataGridView被填充時,而不是顯示包含在'group'中的ID,我希望它顯示組的名稱。是否有某種類型的VB.net「魔術」可以做到這一點,還是我需要自己交叉引用數據?

這裏是的2個表是什麼樣的擊穿:

table1的
ID
組(這保持在表2中列id的值)
重量
LAST_UPDATE

表2
id
description(這是我想要在DGV中顯示的內容)。

順便說一句 - 我正在使用Visual Studio Express。

+0

這與VB.NET沒有任何關係。 DataGridView,DataTable等不是VB.NET的一部分。它們是.NET Framework的一部分。 – 2010-06-15 07:06:14

回答

0

我想說最簡單的方法是做到這一點。

  1. 首先,確保你有一個一對多的關係Table 1和Table之間建立(與父母爲表2的id柱和兒童如表1的group列)。
  2. 在表1中,添加「group_description」列並將其Expression屬性設置爲Parent.description

你這樣做可能是這樣的代碼:

' In case you do not have this relation set up already: ' 
Dim relation = New DataRelation(_ 
    "table2_table1", _ 
    table2.Columns("id"), _ 
    table1.Columns("group") _ 
) 

dataSet1.Relations.Add(relation) 

' This new column will automatically be updated with the description column ' 
' from table2. '  
Dim groupDescriptionColumn = table1.Columns.Add(_ 
    "group_description", _ 
    GetType(String) _ 
) 

groupDescriptionColumn.Expression = "Parent.description" 

那麼你總是隱藏自己的原group列,如果你不希望它是可見的。