Navigating DataGrids: How to access information in Child Tables

  • Thread starter Thread starter Gregory Persson
  • Start date Start date
G

Gregory Persson

I am using a Windows Forms DataGrid to display a DataSet with two tables
using a parent / child relationship. I set this up with the following code:

parentColumn = ds.Tables["Parent"].Columns["ID"];
childColumn = ds.Tables["Child"].Columns["ID"];
dr = new DataRelation("RelParentChild",parentColumn, childColumn);
ds.Relations.Add(dr);

grid.SetDataBinding( ds, "Parent" );

* I need to access information from the Child Table when the user
double-clicks it.

gridWIP.CurrentRowIndex stores the value of the Parent Table and does not
change while the Child Table is being displayed.

Furthermore, this.BindingContext[ dsWIP, "Child" ] appears to be useless:
None of the events fire and the Current & Position values never change.

I can probably access the information I need using the MouseDown event & the
HitTest, but I need to be able to determine which Table the DataGrid is
showing at the time, and I'm not sure how to do that either.

Any suggestions or links to futher reading would be appreciated.

Sincerely,
Greg Persson
 
Thanks for your help Pete, I've managed to figure it out.

The problem I was having could be restated this way:

A Parent-Child DataSet is bound to a SINGLE Windows Forms DataGrid via the
following code:
grid.SetDataBinding( dataSet, parentTableName );

Accessing the currently selected row of the Parent Table is easily
accomplished by:
this.BindingContext[ dataSet, "ParentTable" ].Current;

What is the appropriate BindingContext to access the currently selected row
of the Child Table?

The answer is:
this.BindingContext[ dataSet, "ParentTable.RelationshipName" ].Current;

For a multi-level DataSet you can do:
this.BindingContext[ dataSet,
"ParentTable.Rel1.Rel2.Rel3.Rel4"].Current;

My confusion was based on several factors:
#1 Most Master Detail samples assume you only display 1 table per grid.
#2 Most Master Detail samples assume you only want to display the data in
the grid, not interact with it.
#3 I haven't been able to find a truly great explaination of the inner
workings of Data Binding, especially to DataTables ( which seem to be the
most complex ).

Sincerely,
Greg Persson
MCP
 
Back
Top