How do I bind a datagridview to filtered child collection in the UI ? using a binding source for the parent BO and another for the child collection
Thanks
Ellie
This is unfortunately a bit tricky.
Normally when you drag objects from the data sources window onto your form, Visual Studio actually binds the child bindingsource to the parent bindingsource. Not to your object, but to the parent bindingsource!
And that's fine, but it only works if you want that child bindingsource to bind directly to the parent business object's property. Which you do NOT want to do if you want to get in the middle and sort the values.
So instead what you need to do is drag the parent and child object onto the form - but make sure to drag the child type as a root node of the data sources window, not as a child of the parent.
Then handle the current item changed event on the parent bindingsource, and manually set the datasource of the "child" bindingsource to the sorted/filtered/whatever view of the child list.
If i wanted to have the child filtered list as a property of the parent bo would that be ok as I am not constantly refiltering, only when the bos are loaded. If it is, how do i set that up ?
Thanks
Ellie
No I need them all, basically I have a Pay BO which has Child Line Items. I want to filter for the UI the different types of Line Items. They are all used in the Child BO, the need for splitting has to do with formatting and excluding particular types eg. Entitlements from the Payment types of Wages, Deductions, Tax etc. Any suggestions would be most welcome
Thanks
Ellie
I don’t understand. The filter occurs one time only, but
very late in the process? Or the filter changes during the lifetime of the
object?
I already told you how to handle the case where the filter
changes during the lifetime of the object.
If the filter occurs late in the process, so you can’t do
it in the data access layer, then I would still use the technique I described
earlier – binding the objects to the UI both as root objects, and
handling the current item changed event in the parent’s bindingsource to
filter/bind the child list.
Rocky
Wal972:If i wanted to have the child filtered list as a property of the parent bo would that be ok as I am not constantly refiltering, only when the bos are loaded. If it is, how do i set that up ?
Setting aside Rocky's comments for the moment, I think the answer to this is "no". IIRC, FilteredBindingList does not play well with serialization - it's designed primarily as a UI construct, and won't travel across the wire. I suppose you could have the "real" child collection as one parent property, and the filtered collection as another. But you'd have to mark the filtered property as NonSerialized and NotUndoable, and then re-hook it during your OnDeserialized call. You'd also have to be careful how you set that up, otherwise you'll end up with two copies of your child collection, and you'll end up with other, weirder errors.
HTH
- Scott
Copyright (c) Marimer LLC