How do you properly "bind" a child collection to a grid on a form?

How do you properly "bind" a child collection to a grid on a form?

Old forum URL: forums.lhotka.net/forums/t/4591.aspx


rsbaker0 posted on Monday, March 31, 2008

What we have typically seen in designing forms is that a grid for a child collection on a form has its own binding source. So you have two binding sources, one for the parent object whose properties are bound to fields on the form, and then a separate binding source for the child.

We've been "manually" setting the datasource property of the child grid binding source when the parent object is passed in, which works on the surface, but this isn't a true binding per se. There are situations in which this isn't working right (if you save or cancel editing on the parent, for example, we aren't updating the child grid).

What is the "right" way to do this?

ERodewald replied on Monday, March 31, 2008

Can you provide more info?  (Framework version, language((xaml?c#?), etc).  The forms data provider can be bound to the entire collection, then individual controls can be bound to child objects.  Or you can set the datacontext of a parent layout control to the child object.  Depends on how you set up your child object.

rsbaker0 replied on Monday, March 31, 2008

It's C#, Winforms, .NET 3.0 (although we just moved to this recently and haven't made many changes specific to 3.0).

Here is an example of where we are setting the data source for a form. The form shows a Task (the root object), and has fields bound to it. The Task BO has a child list of TaskItems, which I'm showing in a grid on the same form.

(Sorry about the formatting - not sure how to get code to look good here)

protected override void UpdateBindingSource(BindingSource src) {

if (src.Current is Task)

{

this.taskBindingSource.DataSource = src.Current;

this.taskItemBindingSource.DataSource = ((Task)src.Current).TaskItems;

}

}

 

ERodewald replied on Tuesday, April 01, 2008

I probably wouldn't be the most qualified to answer that, since I work in WPF rather than Winforms and I am sure there are hundreds here who work in winforms. GL :-)

rsbaker0 replied on Wednesday, April 02, 2008

OK, I have looked at an analogous situation in Project Tracker, which is the ResourceEdit form.  The assignments at the bottom are the collection being shown in a grid.

The grid in ProjectTracker uses the same binding source as the form, with a data member being provided to tell it which Property implements the child collection.

Unfortunately, our BO objects currently implement ICustomTypeDescriptor, which means that I can't do this (at least not at design time) the same way. Once I have an actual BO instance at runtime, then I can set the "DataMember" field on the grid.

Otherwise (when the data source of the binding source is just the Type provided at design time) it tells you that it can't resolve the property name. This is some sort of bug or limitation in .NET with regards to objects that implement ICustomTypeDescriptor.

I can make it work by doing what ProjectTracker does at runtime, but it also works if I just set the data source of the child collection binding source directly using the child collection property. Any reason I shouldn't do this?

Copyright (c) Marimer LLC