The old two ListBoxes conundrum..

Posts   
 
    
Meteor
User
Posts: 67
Joined: 06-Apr-2007
# Posted on: 16-Feb-2009 07:19:23   

I am using LLBLGen 2.6, adapter, Winforms application, with a MySql database.

I am showing a dialog to allow users to add Items to a related collection of an object. The items can only be chosen from a list of available items. The main item loaded with the form is a Route (train route). There are two listboxes, showing stations already assigned to the route, and stations available to assign.

I want the users to be able to select from the list of available stations and press an 'Add' button (to add stations to the route), or press a 'Remove' button to do the reverse.

The database structure contains a Routes table, a RouteStations table (with FK_Route, FK_Station and StationOrder), and a Stations table.

I can initially get the list of Stations for the Route by accessing Route.StationCollectionViaRouteStations - and then set that as the DataSource of the listbox.

I'm assuming there's an easy way of applying a filter to the DefaultView of the complete list of Available stations (for the other listbox) using Route.StationCollectionViaRoute? That's my first question - how to apply that filter in memory, and to reapply it every time the items in the other listbox change.

Given that the Order of the stations on a Route is determined by the linking table ('RouteStations.StationOrder'), how would I maintain an ordered list of station objects? I'm thinking the best way would be to create a dummy custom order field for the Station Entity, just to use during this operation. So my second question is, how could I add a custom field and then use it in a 'new SortExpression' statement on the View? confused

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 16-Feb-2009 10:43:45   

I think the following thread is relevant to your question: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=11877

Meteor
User
Posts: 67
Joined: 06-Apr-2007
# Posted on: 16-Feb-2009 22:52:48   

Thanks Walaa, but unfortunately that thread isn't much help, and the sole example for MemberPredicate in the LLBLGen Help isn't much use to me either.

You see it's not so much a related entity that I'm trying to filter on (or I suppose it could be - using the FK in the linking table), but I'm trying to filter the defaultview of a collection of Entities (stations) based on the contents of another collection of stations (WHERE NOT IN...). Do you know what I mean?

Once I have allowed the user to choose from the list of available and re-order the list to their specifications, then I'll run through and add/remove values from the linking object (RouteStations).

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 17-Feb-2009 09:55:32   

but I'm trying to filter the defaultview of a collection of Entities (stations) based on the contents of another collection of stations (WHERE NOT IN...). Do you know what I mean?

And for that, you should collect the PK values from the other collection into an array, and use an EntityView to filter the main collcetion (the available entities collcetion). Using a FieldCompareSetPredicate. And bind to that EntityView.

Meteor
User
Posts: 67
Joined: 06-Apr-2007
# Posted on: 17-Feb-2009 22:34:43   

Thanks.

For future reference, I ended up using this code:


int[] keys = new int[routeStationsBindingSource.Count];
for (int i = 0; i < routeStationsBindingSource.Count; i++)
{
    keys[i] = ((StationEntity)routeStationsBindingSource[i]).Key;
}
IPredicate filter = new FieldCompareRangePredicate(StationFields.Key, null, true, keys);
((EntityCollection<StationEntity>)availableStationsBindingSource.DataSource).DefaultView.Filter = filter;

I created a dummy field on the station entities to hold the station order, and used that to move them up and down in the ListBox. When they hit Save on the form, I ran through the list and created new RouteStation entities (on the parent Route) using the station keys and the station orders.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 17-Feb-2009 22:39:07   

Thanks for updating...sunglasses