views:

16

answers:

1

I have a DataGridView that is being filled with data from a table. Inside this table is a column called 'group' that has the ID of an individual group in another table.

What I would like to do, is when the DataGridView is filled, instead of showing the ID contained in 'group', I'd like it to display the name of the group. Is there some type of VB.net 'magic' that can do this, or do I need to cross-reference the data myself?

Here is a breakdown of what the 2 tables look like:

table1
id
group (this holds the value of column id in table 2)
weight
last_update

table2
id
description (this is what I would like to be displayed in the DGV.)

BTW - I am using Visual Studio Express.

A: 

I'd say the simplest way would be to do this.

  1. First, ensure you have a one-to-many relationship set up between table1 and table2 (with parent as table2's id column and child as table1's group column).
  2. In table1, add a "group_description" column and set its Expression property to Parent.description.

Your code for doing this might look like this:

' 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"

Then you could always hide your original group column, if you don't want it to be visible.

Dan Tao