Click here to Skip to main content
6,595,854 members and growing! (19,008 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

Implementing a strongly typed collection with sort/filter/GetChanges features

By Hayder Marzouk

A strongly typed collection that implements CollectionBase and IBindingList interfaces with interesting features (sort, filter, getchanges).
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:17 Apr 2005
Updated:6 Jul 2005
Views:82,673
Bookmarked:33 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 4.88 Rating: 3.75 out of 5
3 votes, 15.0%
1
1 vote, 5.0%
2
2 votes, 10.0%
3
2 votes, 10.0%
4
12 votes, 60.0%
5

Introduction

In my first steps with .NET, I was impressed by the dataset features. Data access was very simple to use/ to bind on Windows forms components such as DataGrid or ComboBox. But with datasets we are far from OOP. We are always working with Tables (DataTable), rows and columns. So when I began to create my own business objects, all became so difficult: data access problems, binding problems and collection problems. I needed a collection of each business objects that presents the DataSet/DataView features (Sorting, Filtering and the DataSet's GetChanges feature) and it must not be a dataset.

I found many discussion about Collection / Strongly Typed Collections but no one presents all features I needed. So I decided to develop my own custom collection that implements the needed interfaces and functions to be simple to use with Windows forms and also in data access.

Features

  • DataBinding

    The custom collection inherits from CollectionBase abstract class and implements IBindingList interface and can be bound on all controls of Windows forms (datagrid, combo, list,...).

  • Sorting

    The sorting feature is implemented in the IBindingList Interface. So you can sort a column in a DataGrid by clicking on the header of the column.

    Sorting is accessible with a Sort method that accepts a string as parameter ( like the property of the DataView).

  • Filtering

    Dataview presents an very useful feature specially when bound to a DataGrid: Filtering. The custom collection offers too the same feature via the ItemFilter property (RowFilter in the DataView). You can set to the ItemFilter property a string like "Name = Test" or "BirthDate > 01/01/1980" and the collection is updated to display only items that respond to the criteria.

  • GetChanges feature

    The custom collection is based on an abstract object called BaseObject which have a property called ObjectState that can be UnChanged, Added, Deleted or Changed. So any action you do on the collection is stored and you can have the 'List of Changes" made by the user on the collection of items.

    So any business object you need to implement in a collection must inherit from BaseObject.

Implementation

To create your own custom collection, you need a business object that inherits from BaseObject class. For example, if you create a customer class which present a Name property, this code is needed :

public class Customer : BaseObject
    public Customer()
    {
        //

        // TODO: Add constructor logic here

        //

    }
    private string TmpName;
    public string Name
    {
        get{return TmpName;}
        set
        {
            if (base.Compare(TmpName, value) !=0)
            {
                TmpName = value;
                SetDirtyFlag(); // To set the isdirty property of the object to true

            }

    }

Then create the Customers class which inherit from abstract class CustomCollection. Code will be like this:

public class Customers: CustomCollection
{
   public Customers()
   {
       // define the type of the collection items to Customer

       this.ItemType = typeof(Customer); 
   }
   //indexer that overides the default indexer

   public new Customer this[int index]
   {
      get
      {
         return (Customer)(base[index]);
      }
      set
     {
         base[index] = value;
     }
}
// GetChanges Method that overrides the default one 

//to have a Customer Collection in return

    public new Customers GetChanges()
    {
        return (Customers) base.GetChanges();
    }
}

The demo project includes also source code of CustomCollection and BaseObject.

Conclusion

With this CustomCollection, no need to work with DataSets and DataViews. It combines the features of both of these components and we can work with our business objects. So data access can be easier, databinding in Windows forms and web forms is assured and performance is much better than dataset when filling the custom collection from a datareader.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Hayder Marzouk


Member
MCSD Asp.Net certified developer
Occupation: Web Developer
Location: Tunisia Tunisia

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 51 (Total in Forum: 51) (Refresh)FirstPrevNext
GeneralFind? Pinmemberble0t7:28 26 Oct '06  
Questionasp.net version? Pinmembercxjc6:38 23 Aug '06  
QuestionNot Case sensative? PinmemberBigJoe71415:16 1 Jun '06  
GeneralVB.Net Version Pinmembersrinum6:32 28 Feb '06  
QuestionWhat about design-time support for data binding PinmemberAlisterTheDog9:55 14 Jan '06  
GeneralSerialization? Pinmemberbelchski10:22 14 Nov '05  
GeneralRe: Serialization? PinmemberAshwin Seshadri14:06 14 Nov '05  
GeneralRe: Serialization? Pinmemberbelchski14:20 14 Nov '05  
GeneralRe: Serialization? PinmemberJohny Nguyen17:23 23 Nov '05  
GeneralRe: Serialization? PinmemberPatrick Hatton7:00 21 Dec '05  
GeneralRe: Serialization? Pinmembersrinum10:02 23 Feb '06  
GeneralRe: Serialization? Pinmembersrinum11:37 23 Feb '06  
GeneralRe: Serialization? PinmemberAtlas20025:01 28 Feb '06  
GeneralI greatly increased performance!!!! PinmemberAlexei Tarnakin1:47 18 Oct '05  
GeneralRe: I greatly increased performance!!!! PinsussAnonymous2:55 18 Oct '05  
GeneralRe: I greatly increased performance!!!! PinmemberAlexei Tarnakin6:28 18 Oct '05  
GeneralProblem with clear method PinmemberAlexei Tarnakin23:01 28 Sep '05  
GeneralRe: Problem with clear method PinmemberJohny Nguyen0:49 18 Oct '05  
GeneralRe: Problem with clear method PinmemberAlexei Tarnakin1:31 18 Oct '05  
GeneralRe: Problem with clear method PinmemberJohny Nguyen19:52 19 Oct '05  
GeneralRe: Problem with clear method PinmemberAlexei Tarnakin1:36 20 Oct '05  
GeneralWhere is the end? PinmemberAlexei Tarnakin9:31 23 Aug '05  
GeneralRe: Where is the end? PinmemberHayder Marzouk23:29 23 Aug '05  
Generalimprovements PinmemberAlexei Tarnakin5:51 17 Jun '05  
GeneralRe: improvements PinmemberAlexei Tarnakin6:15 17 Jun '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jul 2005
Editor: Smitha Vijayan
Copyright 2005 by Hayder Marzouk
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project