Filtered List Child Collection

Filtered List Child Collection

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


Wal972 posted on Sunday, June 29, 2008

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

RockfordLhotka replied on Tuesday, July 01, 2008

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.

Wal972 replied on Tuesday, July 01, 2008

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

RockfordLhotka replied on Tuesday, July 01, 2008

If you just want it filtered when you load it, I'd do the filtering in the stored procedure or SQL statement, or worst case in the DataPortal_Fetch() method. No sense moving the unwanted rows any further from the hard drive behind the database than necessary.

Wal972 replied on Tuesday, July 01, 2008

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

RockfordLhotka replied on Wednesday, July 02, 2008

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

tmg4340 replied on Wednesday, July 02, 2008

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.  Smile [:)]

HTH

- Scott

stefan replied on Thursday, July 03, 2008

Or what about having a public method that would return a filtered list...
Then you would bind the GUI to the result of a method call...

But I don't know the implication that would have on the underlaying BusinessListBase object (thinking of the EditLevel...). I expect that this way you would have to keep your hands off that list object...


Just a thought

Stefan

Copyright (c) Marimer LLC