CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

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



Comments

Brendan Tompkins said:

Jordan. You converted this new one? Sure! Send it over, and gosh, you're quick!
# March 18, 2005 11:59 AM

Sahil Malik said:

This is good work. I have a few ideas around this myself. When I get time, I will expand on them.
# March 18, 2005 2:56 PM

Sahil Malik said:

fore have no idea how unusable a fabler PC really is!!


you have no idea how unusable a tablet PC really is.
# March 18, 2005 2:58 PM

Brendan Tompkins said:

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....
# March 18, 2005 5:02 PM

Sahil Malik said:

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.
# March 18, 2005 8:02 PM

Ron said:

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
# March 23, 2005 9:46 AM

Michael Thoms said:

Thanks a lot, this is just what i needed! Is there any possibility this is solved within .NET 2.0?
# April 1, 2005 3:52 PM

Simon Evans' Blog said:

# April 23, 2005 3:46 AM

Jack Thomas said:

The vb.net download link is broken.
# August 9, 2005 5:09 PM

Andrew said:

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?
# September 8, 2005 12:58 AM

jokiz said:

i am a fan of custom business objects so i usually have a strongly typed collection inheriting from CollectionBase. ...
# May 11, 2006 12:57 AM

ViM said:

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?

# June 8, 2006 2:58 PM

zac glenn said:

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
# August 2, 2006 10:10 PM

jokiz's blog said:

i am a fan of custom business objects so i usually have a strongly typed collection inheriting from CollectionBase.
# August 11, 2006 7:54 AM

Simon Evans' Blog said:

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

# September 5, 2006 5:28 AM

Victor said:

awesome

# October 27, 2006 4:57 PM

Victor Sinfuente said:

This is a great help

# October 27, 2006 4:58 PM

Ralluf said:

I love you! Many thanx!!

# December 8, 2007 3:16 PM

cialis generic Acheter mastercard said:

27c6899db6:46 Pharmacie en ligne fr.generics-first-hand.com

# December 19, 2007 6:33 PM

online pharmacy said:

0511b9be64:46 Online pharmacy Later in the process, drug companies may seek new patents on the production of specific forms of the

# December 23, 2007 6:28 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors