Binding the child rows of a DataRow#

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.

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:

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.

Monday, January 30, 2006 6:22:23 PM (Romance Standard Time, UTC+01:00)
Its pretty obnoxious that this is the solution to this issue. I find myself in the same position. Its a terrible solution if you have lots of row.

I'm improving on it slightly by leaving the DefaultView sorted on the primary key and using DefaultView.Find to get the DataRowView instead of looping over every record.

Its still a ridiculous solution to what should be a trivial problem.
disco
Monday, March 12, 2007 9:23:36 PM (Romance Standard Time, UTC+01:00)
Thanks for posting this. Apparently it's the same way in .Net 2.0, as I found myself with the same problem today trying to bind child controls to a GridView.
Greg
Comments are closed.
All content © 2012, Jelle Druyts
On this page

Recent Photos
www.flickr.com
This is a Flickr badge showing public photos from Jelle Druyts. Make your own badge here.
Advertising
Top Picks
Statistics
Total Posts: 350
This Year: 0
This Month: 0
This Week: 0
Comments: 530
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.0.7226.0

Sign In