Jelle Druyts .NET Consultant
Just another ignorant weirdo from Antwerp, Belgium trying to make sense out of it all
Either I'm missing something or the ADO.NET team missed a fairly simple scenario... Imagine you have a single DataRow out of a table with a relation to another table. Now you want to bind a DataGrid (e.g. WinForms but that doesn't really matter here) to the child rows of that DataRow. Sounds easy, right?
And sure, there's a handy GetChildRows method that returns an array of child rows for a given relation. Unfortunately, binding a grid to an array will make it display the properties of the DataRow object, not the actual content of the DataRow.
GetChildRows
So you need some way to get something bindable out of that parent row. You might create a new DataTable with the same structure and load it with the child rows but that's pretty cumbersome.
If you have a DataRowView, then you can get a bindable DataView out of it using the CreateChildView method. Although you can't lookup or create a DataRowView directly, you can get it by looping over the DefaultView of the associated table. This is how it could roughly look in code:
CreateChildView
public DataView GetChildView( DataRow parentRow, DataRelation relation ){ // Find the associated DataRowView of the parent data row. foreach( DataRowView rowView in parentRow.Table.DefaultView ) { if( rowView.Row == parentRow ) { // Create a child view through the DataRowView and the relation. return rowView.CreateChildView( relation ); } } // No associated DataRowView found. return null;}
Now the resulting DataView can easily be bound to a datagrid.