Question Displaying data from related table in DataGridView

bufer24

Active member
Joined
Mar 27, 2008
Messages
35
Programming Experience
3-5
I have 2 tables in my dataset:
- Items
- Warehouse

The Items table contains a column named 'WarehouseID'. It tells me which warehouse this item belongs to.

The Warehouse table contains an 'ID' column and a 'Name' column.

Now if I display the contents of Items table in a datagridview I will see only numbers in the WarehouseID column. How can I make the datagridview display the name of the warehouse instead of its ID?
 

Attachments

  • Rel.jpg
    Rel.jpg
    52.5 KB · Views: 33

henryminute

Well-known member
Joined
Jul 24, 2009
Messages
57
Location
UK
Programming Experience
10+
One way to do this is by using a slightly different SELECT statement in the SQL used to fill your DataGridView

The very simplest would be something like:
VB.NET:
SELECT i.ItemID, i.ItemDescription, w.WareHouseName, i.ItemCost
FROM Items i, WareHouse w
WHERE i.WarehouseID = w.WareHouseID

and you could also use an Inner Join, which is slightly more complex to look at but not too difficult.

The problem with either of these methods is that it makes editing the data slightly more complex. So I would only recommend this approach if the data is intended only to be viewed.
 

bufer24

Active member
Joined
Mar 27, 2008
Messages
35
Programming Experience
3-5
that´s OK, but there is a little problem: I don´t use any dataadapter or real database. I read data directly do the Dataset from XML. (DataSet.ReadXml('mydata.xml'))
 

henryminute

Well-known member
Joined
Jul 24, 2009
Messages
57
Location
UK
Programming Experience
10+
In that case, take a look at [ame=http://www.codeguru.com/forum/showthread.php?t=257849] select statement on a dataset - CodeGuru Forums[/ame] scroll down to the 4th (I think) post by "graye". The two links there may provide some ideas.
 
Top Bottom