2010-06-15 89 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列,如果你不希望它是可见的。