Show master column in details datagridview

  • Thread starter Thread starter Jordi Rico
  • Start date Start date
J

Jordi Rico

Hi,
I've been searching quite a lot and can't find any answer.
Let's put an example:

I have 2 tables:

Master:
idMaster int
masterName nvarchar(50)

Details:
idDetails int
idMaster int
detailsName nvarchar(50)
quantity int

I have a datagridview and I want to display data from Details table,
but I also want to display a column with masterName from it's parent
table. This column won't be editable, but I must be able to change
some columns from details (for example quantity) and update database
with this changes. I remark that masterName will be only informative.
I have been searching and can't do this thing. I thought it would be
easier but I'm stuck with this. Any help will be appreciated.

Thanks in advance,

Jordi Rico
 
Jordi said:
Hi,
I've been searching quite a lot and can't find any answer.
Let's put an example:

I have 2 tables:

Master:
idMaster int
masterName nvarchar(50)

Details:
idDetails int
idMaster int
detailsName nvarchar(50)
quantity int

I have a datagridview and I want to display data from Details table,
but I also want to display a column with masterName from it's parent
table. This column won't be editable, but I must be able to change
some columns from details (for example quantity) and update database
with this changes. I remark that masterName will be only informative.
I have been searching and can't do this thing. I thought it would be
easier but I'm stuck with this. Any help will be appreciated.

Step 1: put both tables in the same dataset, if they are not already.

Step 2: define a Relation in that dataset, linking the two tables by idMaster.

Step 3: add a column to the Details table, with an expression displaying a field
from the parent table.

Step 4: add a column to your grid to display that field.

So if you start with

Dim Master As DataTable
Dim Details As DataTable

you can then do

Dim MyDataSet As New DataSet

MyDataSet.Tables.Add Master
MyDataSet.Tables.Add Detail

MyDataSet.Relations.Add( _
New DataRelation("MasterToDetail", _
Master.Columns("idMaster"), _
Details.Columns("idMaster")))

Details.Columns.Add( _
New DataColumn("MasterName", _
GetType(String), _
"Parent(MasterToDetail).masterName"))

Voila. You can now display the MasterName column of the Details table.
 
Back
Top