apply sort to BusinessCollectionBase from code (v1.5)

apply sort to BusinessCollectionBase from code (v1.5)

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


kayakheaven posted on Friday, December 29, 2006

Here is how to apply a sort to a BusinessCollectionBase (v1.5) from code, in case anyone finds the example useful. I didn't find it in the forum when I searched just now. Could be simplied by replacing _list[0] and check of _list.Count with a canonical instance.

using System.ComponentModel;

if (_list.Count > 0)

{

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(_list[0]);

PropertyDescriptor property = properties.Find("name", true);

ListSortDirection direction = ListSortDirection.Ascending;

((IBindingList)_list).ApplySort(property, direction);

}

JoeFallon1 replied on Saturday, December 30, 2006

In 1.x there was a larg community effort to build Sortable CollectionBase (and then FilterableCollectionBase). As I recall, the code would be added to Csla (maybe re-name an existing file so it uses the new code in the file of the same name) and then re-compile Csla. This allowed you to sort and filter any Csla collections. In the old forum there was an Attachments section or something where you could download the code.

In 2.x the idea has changed to a bit. Rather than modify the collection directly, you provide a View of it and then display the View. This View can be sorted or filtered or both. Rocky provides the SortedListView and the Filtered ListView in 2.x. Brian Criswell provides the ObjectListView which does both PLUS has the ability to do multi-property sorting (which Rocky's does not do.)

Joe

 

kayakheaven replied on Monday, January 01, 2007

Hi Joe, could you clarify a couple of points from your post.

You mention "the old forum", is that available anywhere?

CSLA v1.5 download from lhotka.net has SortedCollectionBase included in it, but I don't see anything for FilteredCollectionBase, or did I miss it?

You still have to write code in your own UI form or class to make the sort happen. I didn't find that sample anywhere in this forum. That is what my posting illustrates.

When you talk about the idea changing in v2.x, my understanding is that v1.5 also uses a "view", which maps the sorted ids to the unsorted base collection. Perhaps the v2.x view is different.

Brian Criswell replied on Monday, January 01, 2007

I think what Joe was referring to is that the 1.x lists had the ability to sort themselves.  SortedCollectionBase is the filename, but the class within is SortableCollectionBase.  It provides the sorting functionality for BusinessCollectionBase.  After 1.x had been out for a while, someone released a sorted view class that did stable sorts (and multi-column sorts?).  A filtered view class was also released by someone else.  A VB version is available on the wiki (linked at the top of the main forum page), and I have attached a C# version.

One of my friends also back-ported the ObjectListView to CSLA 1.x by removing the reference to IBindingListView and making a few other tweaks so that it would compile.

Brian Criswell replied on Monday, January 01, 2007

Go figure I never attached the file.

As a side note, one thing a lot of people do is create their own ObjectBase, ListBase, ROObjectBase and ROListBase classes.  This would allow you to have an ApplySort(string, ListSortDirection) method in ListBase that could take a property name and internally convert it to the correct property descriptor, allowing you to implement the functionality once for all of your lists.

I do not have a copy of the back ported ObjectListView.  I will see if I can duplicate what my friend did.

Brian Criswell replied on Thursday, January 11, 2007

If you want, you can try it a first version of the ObjectListView backported to CSLA 1.x.

kayakheaven replied on Thursday, January 11, 2007

Thanks Brian, really appreciate your help. I had a quick read of the source code. Just so I don't misunderstand, is this a full port beta version or a partial port with some features not yet ported?

Brian Criswell replied on Thursday, January 11, 2007

All the functionality should be there.  The changes include:

Copyright (c) Marimer LLC