Brendan Tompkins

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Sort/Filter Your Business Entity Collections with a NEW CollectionView

You’ve probably heard a lot of talk about why you should use Business Entities rather than DataSets.  There are good arguments for and against using Typed DataSets as Business EntitiesAnd even very recently, there’s some people changing their minds on the issue.

I’m generally for the raw Business Entity approach, typically from the business layer up, especially when exposing data at the end of Web Services. Sahil has promised to make me see the Typed DS light, but right now, I’ve actually developed quite a bit of non-typed DataSet BE objects in my framework.  If you’re like me and gone down this route, you may have found that once you get the hang of it, it’s not too hard to DataBind your BEs to your UI controls.  Then you may have created some strongly-typed collections containing your BEs, and naturally, you’ve needed to bind these collections to a DataGrid, which is easy. So you’re really proud of yourself, and you show your boss this great work, and they say to you…

“Great. Nice Grid. What happened to my sorting?” 

You then you mumble something about how your collections aren’t sortable, but they are better than DataSets nonetheless, because you can add your own business logic to them easily, and they’re more generic, and they might work better on the wire, and you look up at them and with a blank stare they say to you…

“Great. Nice Grid. Um… What happened to my sorting again?”

So what are you gonna do? You could create a DataSet out of your Collection and pass that off to your Grid, but did someone say hack? You could add sorting to all of your collection objects, but did someone say tedious

Do I have a perfect solution? No. But I do have a solution that works pretty well for me, and lets me add sorting and filtering to my DataGrid-bound BE collections.  A few months back, I posted about some code I’d worked on to allow  Sort and Filter Strongly-Typed Collection Classes

Using this class is easy. Say you want to bind your strongly typed collection to a DataGrid, and enable sorting and filtering. Simply pass your collection class to the constructor of the CollectionView, and bind your grid to the CollectionView instance instead.

Like I said, I’ve been using it and it’s working pretty well for me.  I’ve recently added sorting and filtering of child object’s properties, and some better handling of null values.  So without further ado, here’s the new download.

UPDATE - 7/25/05 - Jordan Bowness has translated the code into VB. You can download that file here. Thanks Jordan!

Enjoy!

-Brendan


Posted Fri, Mar 18 2005 4:11 AM by Brendan Tompkins

[Advertisement]

Comments

Jordan wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Fri, Mar 18 2005 11:54 AM
I have this converted over to vb.net as well. If you'd like me to email you the class Brendan, please drop me a line at [jb _at_ bizeworld _dot_ com]

Brendan Tompkins wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Fri, Mar 18 2005 11:59 AM
Jordan. You converted this new one? Sure! Send it over, and gosh, you're quick!
Sahil Malik wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Fri, Mar 18 2005 2:56 PM
This is good work. I have a few ideas around this myself. When I get time, I will expand on them.
Sahil Malik wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Fri, Mar 18 2005 2:58 PM
fore have no idea how unusable a fabler PC really is!!


you have no idea how unusable a tablet PC really is.
Brendan Tompkins wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Fri, Mar 18 2005 5:02 PM
Sahil.. I know! It's not very cool to say so, but I tried using one of those things recently, and the experience sucked pretty bad... But then again, my handwriting sucks....
Sahil Malik wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Fri, Mar 18 2005 8:02 PM
My handwriting is so bad, I can't read it 2 minutes after I wrote it. But still, all that means is - hey that cool device - is bludy useless !!

I mean, I do see a few uses for it, but I see it replacing books at best.
Ron wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Wed, Mar 23 2005 9:46 AM
What about re-filtering a filtered list? Calling ApplyFilter clears the first filter. Using this code causes an exception:

CollectionView view2 = new CollectionView(view1); view2.ApplyFilter("Name", name);

"Collection was modified; enumeration operation may not execute."

Adding this code to ApplyFilter makes it possible:

ArrayList newFilteredList = new ArrayList();

foreach (object obj in ((isFiltered) ? filteredList : baseList))
{
do filter...
}

filteredList = newFilteredList;

Is there a better way? I inlined the test of isFiltered becuase I didn't want another temp IList object.

I think there needs to be more FilterOperands too:

public enum FilterOperand
{
Equals,
NotEquals,
Includes,
NotIncludes,
StartsWith,
EndsWith,
StartsWithIgnoreCase,
EndsWithIgnoreCase
}

Lastly, I don't think FilterOperand.Equals supports nulls properly. This seemed to fix it for me:

case FilterOperand.Equals:
if (compareValue==null && f==null)
{
filteredList.Add(obj);
}
else if (f.Equals(compareValue))
{
filteredList.Add(obj);
}
break;

My applogizes if the code didn't paste in correctly.

Cheers,
Ron
Michael Thoms wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Fri, Apr 1 2005 3:52 PM
Thanks a lot, this is just what i needed! Is there any possibility this is solved within .NET 2.0?
Simon Evans' Blog wrote Data binding to objects using the ObjectDataSource control in ASP.net 2.0
on Sat, Apr 23 2005 3:46 AM
Jack Thomas wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Tue, Aug 9 2005 5:09 PM
The vb.net download link is broken.
Andrew wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Thu, Sep 8 2005 12:58 AM
Doesn't Work??

I downloaded Rockford Lhotka Sorting the Unsortable Collection and used your collectionview code in this example.
It seems to only sort once on the first load.

In the example if I click the column header1 of a datagrid it will sort. If I then went and clicked the other column header2 it would do nothing??

is there something I'm doing wrong?
jokiz wrote CollectionView
on Thu, May 11 2006 12:57 AM
i am a fan of custom business objects so i usually have a strongly typed collection inheriting from CollectionBase. ...
ViM wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Thu, Jun 8 2006 2:58 PM
Brendan & Jordan,

Great work! I used the Vb.NET version of the class & it's works like a charm. I'm binding a sortable datagrid to the collection. But when I sort a filtered list, I loose the filtering. Am I doing something wrong?

zac glenn wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Wed, Aug 2 2006 10:10 PM
This is great! Thanks.

Regarding performance is it better to go with strongly typed datasets than
list that will then be converted to collectionviews? I understand that if one's DAL is already creating strongly typed lists then the collectionview makes since.....

Thanks again
jokiz's blog wrote CollectionView
on Fri, Aug 11 2006 7:54 AM
i am a fan of custom business objects so i usually have a strongly typed collection inheriting from CollectionBase.
Simon Evans' Blog wrote Data binding to objects using the ObjectDataSource control in ASP.net 2.0
on Tue, Sep 5 2006 5:28 AM

Introduction If there’s one thing I like in life it’s a subject that brings out passionate

Victor wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Fri, Oct 27 2006 4:57 PM

awesome

Victor Sinfuente wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Fri, Oct 27 2006 4:58 PM

This is a great help

Ralluf wrote re: Sort/Filter Your Business Entity Collections with a NEW CollectionView
on Sat, Dec 8 2007 3:16 PM

I love you! Many thanx!!

Devlicio.us