Wednesday, November 23, 2005

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.

9 comments:

  1. Did you ever get a chance to write up your BindingList class? I'm working on the same issue, and it would be nice to see what someone else has done!

    ReplyDelete
  2. No I haven't...

    But I think I'll have some time as soon as this week to quickly write something up...

    (Yeah, I know... Promises, promises... ;)

    ReplyDelete
  3. David,
    I just quickly knocked out an example project which shows how I'm using BindingList with sorting, etc...

    If you shoot me an email, I'll send it to you, if you're still interested.

    gduncan411 [at] hotmail[thedot]com

    ReplyDelete
  4. Please excuse my ignorance, but how do you explicitly invoke the Sort from code when it is not bound to a datagrid?

    ReplyDelete
  5. hey Greg

    I just wanted to say thank you for putting this together... I've implemented it on my project, and it works like a dream. I actually created a generic class out of it (GenericBindingList) that i can use to store collections of any types of objects, bind directly to DGV, and then it'll sort them, as well as allow inline editing as its bound directly to the control.

    Brilliant stuff :)

    Love your work..
    Greg

    ReplyDelete
  6. Hi greg i would be very interested in your generic bindinglist class for a little work i'm doing right now. Seems nice and would be greatly appreciated right now.

    djsinae (at) gmail.com


    tx
    Alex

    ReplyDelete
  7. @Alexandre
    Email sent with the URL for the public SkyDrive folder.

    It's the same one in the Update #2...

    Thanks

    ReplyDelete
  8. the links are dead yet :(

    ReplyDelete
  9. @Sebastian Thanks for the heads up. Looks like the SkyDrive URLs changed. Link updated (to http://sdrv.ms/VlDMUd)

    ReplyDelete

NOTE: Anonymous Commenting has been turned off for a while... The comment spammers are just killing me...

ALL comments are moderated. I will review every comment before it will appear on the blog.

Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...

I reserve, and will use, the right to not approve ANY comment for ANY reason. I will not usually, but if it's off topic, spam (or even close to spam-like), inflammatory, mean, etc, etc, well... then...

Please see my comment policy for more information if you are interested.

Thanks,
Greg

PS. I am proactively moderating comments. Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...