【问题标题】:'cross-referencing' DataTable's'交叉引用' DataTable's
【发布时间】:2011-03-03 20:30:43
【问题描述】:

我有一个 DataGridView,它正在填充表中的数据。此表内部有一列称为“组”,其中包含另一个表中单个组的 ID。

我想要做的是,当填充 DataGridView 时,我希望它显示组的名称,而不是显示“组”中包含的 ID。是否有某种类型的 VB.net 'magic' 可以做到这一点,还是我需要自己交叉引用数据?

以下是这 2 个表格的详细信息:

table1
身份证
组(这包含表 2 中列 id 的值)
重量
最后更新

表2
身份证
描述(这是我希望在 DGV 中显示的内容。)

顺便说一句 - 我正在使用 Visual Studio Express。

【问题讨论】:

  • 这与 VB.NET 毫无关系。 DataGridView、DataTable 等不是 VB.NET 的一部分。它们是 .NET Framework 的一部分。

标签: vb.net ado.net dataset


【解决方案1】:

我想说最简单的方法就是这样做。

  1. 首先,确保您在 table1 和 table2 之间设置了一对多关系(父作为 table2 的 id 列,子作为 table1 的 group 列)。
  2. 在table1中,添加一个“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 列,您可以随时隐藏它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-15
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    相关资源
    最近更新 更多