Display mutliple 1-1 tables in DataGrid?

G

Guest

I’ve read a number of article/books on ADO.NET and the DataGrid and nobody
seems to address displaying multiple tables at once (not master/detail).

I’ve got two tables that have a 1-1 relationship based on a primary key.
Basically, think of it as one logical record (Customer) with some data in
table 1 and other data in table 2. I have these loaded into a DataSet and I
have a relation defined between them.

However, I cannot find any way to make the DataGrid display data from both
tables at the same time. I want to see the following:

Name Address
John 663 Main
Sally 223 Oak
…

Where Name is coming from table 1 and Address is coming from table 2.

Is this possible with the DataGrid control(s)? (I can certainly look at
writing a custom display control but I’d hate to reinvent the wheel)
 
G

Guest

I don't believe you are going to be able to do it the way your are
explaining. You are wanting to set the DataGrid.DataSource = Table, right? To
get this to work, you are going to have to modify one of the tables to
contain a DataColumn with an Expression pointing to the other tables column
of interest.
TableA (A DataColumns)
TableB (B DataColumns)(A Expression DataColumns)

Here is a quote from the MSDN DataColumn.Expression property documentation:
PARENT/CHILD RELATION REFERENCING

A parent table may be referenced in an expression by prepending the column
name with Parent. For example, the Parent.Price references the parent table's
column named Price.

A column in a child table may be referenced in an expression by prepending
the column name with Child. However, because child relationships may return
multiple rows, you must include the reference to the child column in an
aggregate function. For example, Sum(Child.Price) would return the sum of the
column named Price in the child table.

If a table has more than one child, the syntax is: Child(RelationName). For
example, if a table has two child tables named Customers and Orders, and the
DataRelation object is named Customers2Orders, the reference would be:
Avg(Child(Customers2Orders).Quantity)


if you are using typed DataSets, this can be done using the GUI interface.

Not a magical solution, but one that is solved by a lot of typing.
 
G

Guest

Hmm. Not good.

First off, I do not have multiple children and the data is text so there is
no aggregate function that I can use in referencing the child item.

What I'd like to be able to do is set the DataGrid.DataSource = myDataSet
(which contains both tables) and then set up the grid columns like this:

Grid.Column[0].DataTable = "Customers"
Grid.Column[0].DataItem = "Name"
Grid.Column[1].DataTable = "CustomerNotes"
Grid.Column[1].DataItem = "Note"

Sadly, I don't think anyone thought of this when the DataGrid was being
developed.

So, the workaround (solution, I guess) I came up with is to add the columns
from table to the schema for table1 and then .Merge the second table into the
first so I end up with one table for the DataGrid. Then, when I need to save
the table 2 data, I take the table being displayed and delete the table1
schema (columns) from it and then write it. Works like a charm since both
tables use the same primary key and I never need to save table 1 (it's
static). Seems like a hack but I think writing a display control that
handled multiple tables/items would be more work :)

Thanks
 
G

Guest

I can't do that since these are being loaded from independent XML files.
Thanks for the response though -- I failed to mention these are not DB
queries.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top