Fun with BindingList(of T) and Bindable, Sortable Collections of Domain Objects
One of the things I really wanted to play with in VB/VS 2005 was data binding with domain objects (i.e. my own business classes and not a dataset/table).
I’m a big fan of using domain objects, where they make sense. I don’t believe in going overboard and requiring/making everything a domain object though. DataSets/Tables are pretty cool now and sure make some things very easy.
That said, I don’t want to have to coerce my perfectly good domain objects into datatables just so I can bind them to a DataGridView.
I looked at implementing IBindingList and what a nightmare. Next…
.Net 2.0 introduces the BindingList(of T) class. This is a great base class to inherit from for creating custom, bindable domain object collections.
With a little help from Sorting the Unsortable Collection, by Rockford Lhotka and Custom Data Binding, Part 2, by Michael Weinhardt, I now have a fully working datasource bindable collection of domain objects. This includes sorting, editing, adding, etc, etc.
To get sorting working, I inherited from BindingList(of T), overrode and implemented two Subs and four Properties. And that’s about it! If you’ve ever looked at supporting sorting with IBindingList you’ll know why I’m so excited.
Michael gets the credit for performing the real magic. His implementation of the PropertyComparer made my work easy.
Next week or so, I’m going to try and write this up in more detail and post it to Code Project.
Update #1 1/9/2005 @ 4:13 PM (PST):
I finally got off my butt and posted a sample of the work I mention above...Check out this post for more information, or see the GotDotNet User Sample, VB.Net Binding List With Sorting Example.
Update #2 4/28/2008 @ 2:45 PM (PDT):
GotDotNet is gone, along with my sample code... sigh... :(
But luckilyI had a backup copy.
For the time being I'm going to post it to a "readable by everyone" SkyDrive folder, Fun With Bindling List Source
Thank you Anna for reminding me to repost this source.