Monday, January 10, 2005

Use BindingList for data bound collections

I finished up another chapter on my book last night. This one was on creating data bound custom business objects and collections. I won't try to reiterate the entire chapter here, but I will pass along one of the key take-aways: use BindingList<T> for custom business object collections if you want to provide rich data binding capabilities in Windows Forms 2.0.

The BindingList<T> generic type is defined in the System.ComponentModel.Collections.Generics namespace in Beta 1, but is getting refactored into System.ComponentModel in Beta 2. Basically this type allows you to create strongly typed collections that implement IBindingList (partially), IList, ICollection, and IEnumerable, with a single line of code:

BindingList<Customer> CustomerCollection = new BindingList<Customer>();

By doing just this, you get a collection that works fine with both Windows Forms and ASP.NET data binding mechanisms, including two-way data binding (presentation and editing) support in Windows Forms, and also will fire ListChanged events whenever items in the collection are added, removed, or replaced. If you want to support events when the contained objects are edited, you will need to implement the INotifyPropertyChanged interface on the object type definition itself (this interface is named IPropertyChanged in Beta 1).

Additionally, if you want to support sorting and searching on the collection, all it takes it to derive a class from your BindingList<T> parameterized type, and override a few base class methods and properties. For example, you could declare a Customer collection and provide searching capability with something like the following:

public class CustomerCollection : BindingList<Customer>
{
   protected override bool SupportsSearchingCore { get { return true; } }

   protected override int FindCore(PropertyDescriptor property, object key)
   {
      for (int i = 0; i < Count; i++)
      {
            Customer c = this[i];
          if (property.GetValue(c) == key)
          {
              return i;
          }
      }
      return -1; // Not found
  
}
}

Very powerful stuff. Sorting is a little more involved, but follows the same process. Override the sort related properties, and provide a sorting algorithm in the ApplySortCore override and you are off and running.

So much easier and more powerful than the old derive from CollectionBase approach. You get type safety, alot of additional functionality, and the efficiency of generic types.



.NET | Languages and Tools

Monday, January 10, 2005 4:09:21 PM (GMT Standard Time, UTC+00:00)
Comments [3]  |  Related posts:
DevConnections Orlando Slides and Demos
Developing Applications with Windows Workflow Foundation LiveLesson
Slides and demos from Cleveland .NET SIG
Silverlight - not just pretty graphics - Cross platform .NET Framework!
Great guidance on using Team Foundation Server for your projects
DevConnections Orlando - a tale of three sessions
Tracked by:
"adipex" (adipex) [Trackback]
"slot machines" (slot machines) [Trackback]
"casino games" (casino games) [Trackback]
"casino" (casino) [Trackback]
"slot machines" (slot machines) [Trackback]
"good forex trading systems" (forex trading systems) [Trackback]
"freeware texas holdem" (freeware texas holdem) [Trackback]
"Online Games poker" (Online Games poker) [Trackback]
"viagra" (viagra) [Trackback]
"online casinos" (online casinos) [Trackback]
"online casinos" (online casinos) [Trackback]
"casino on net" (casino on net) [Trackback]
"cash advance" (cash advance) [Trackback]
"online casino game com" (online casino game com) [Trackback]
"online betting casinos" (online betting casinos) [Trackback]
"Free Downloadable Poker" (Free Downloadable Poker) [Trackback]
"free slot machine software" (free slot machine software) [Trackback]
"pet prescriptions online" (pet prescriptions online) [Trackback]
"slot machines" (slot machines) [Trackback]
"ganancias casinos portales internet" (ganancias casinos portales internet) [Trackback]
"best online sports casino to bet" (best online sports casino to bet) [Trackback]
"texas holdem poker rule" (texas holdem poker rule) [Trackback]
"online blackjack" (online blackjack) [Trackback]
"phentermine online" (phentermine online) [Trackback]
"juego gratis linea" (juego gratis linea) [Trackback]
"spielautomat" (spielautomat) [Trackback]
"black casino jack online poker video" (black casino jack online poker video) [Trackback]
"effective diet pill" (effective diet pill) [Trackback]
"ambien cheapest consultation free online pharmacy" (ambien cheapest consultatio... [Trackback]
"tamiflu purchase" (tamiflu purchase) [Trackback]
"palm 505 download texas holdem free" (palm 505 download texas holdem free) [Trackback]
"progressive slot machines" (progressive slot machines) [Trackback]
"cash advance" (cash advance) [Trackback]
"low carb diet pills" (low carb diet pills) [Trackback]
"online pharmacy without percsription" (online pharmacy without percsription) [Trackback]
"no deposit online casino coupon codes" (no deposit online casino coupon codes) [Trackback]
"texas holdem secrets" (texas holdem secrets) [Trackback]
"jo.hawaiicity.com link online phentermine" (jo.hawaiicity.com link online phent... [Trackback]
"forex course trading" (forex course trading) [Trackback]
"dish network" (dish network) [Trackback]
"neteller online casinos" (neteller online casinos) [Trackback]
"856" (856) [Trackback]
"1936" (1936) [Trackback]
"hot housewives" (hot housewives) [Trackback]
"freeware keno games" (freeware keno games) [Trackback]
"soma" (soma) [Trackback]
"butalbital" (butalbital) [Trackback]
"didrex" (didrex) [Trackback]
"alprazolam" (alprazolam) [Trackback]
"hydrocodone online" (hydrocodone online) [Trackback]
"casino black jack" (casino black jack) [Trackback]
"document management" (document management) [Trackback]
"pharmacies online" (pharmacies online) [Trackback]
"buy soma" (buy soma) [Trackback]
"buy cipro" (buy cipro) [Trackback]
"generic butalbital" (generic butalbital) [Trackback]
"sandia casino" (sandia casino) [Trackback]





















Sign In
Copyright © 2006-2007 Brian Noyes. All rights reserved.
designed by NUKEATION STUDIOS