Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
3.75/5 (20 votes)
6 Jul 20052 min read 181.1K   1.2K   41   51
A strongly typed collection that implements CollectionBase and IBindingList interfaces with interesting features (sort, filter, getchanges).

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 :

C#
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:

C#
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


Written By
Web Developer
France France
MCSD Asp.Net certified developer

Comments and Discussions

 
GeneralRe: Problem with clear method Pin
Johny Nguyen19-Oct-05 18:52
Johny Nguyen19-Oct-05 18:52 
GeneralRe: Problem with clear method Pin
Alexei Tarnakin20-Oct-05 0:36
Alexei Tarnakin20-Oct-05 0:36 
QuestionWhere is the end? Pin
Alexei Tarnakin23-Aug-05 8:31
Alexei Tarnakin23-Aug-05 8:31 
AnswerRe: Where is the end? Pin
Hayder Marzouk23-Aug-05 22:29
Hayder Marzouk23-Aug-05 22:29 
Generalimprovements Pin
17-Jun-05 4:51
suss17-Jun-05 4:51 
GeneralRe: improvements Pin
Alexei Tarnakin17-Jun-05 5:15
Alexei Tarnakin17-Jun-05 5:15 
GeneralRe: improvements Pin
Hayder Marzouk17-Jun-05 7:44
Hayder Marzouk17-Jun-05 7:44 
GeneralRe: improvements Pin
Hayder Marzouk20-Jun-05 1:39
Hayder Marzouk20-Jun-05 1:39 
Always with performance.
There is a tiny problem in the ItemFilter property. 2 lines of code are missing.
In the set accessor of the ItemFilter Property add the following line in the beginning : this.IsFiltering =true;
This line disable the auto refresh when the collection is bound to a datagrid.

and in the end add :
this.IsFiltering = false;
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, 0,0));
To reEnable AutoRefresh.

Performance will be increased by 10000 when the collection is bound.

I didn’t understand very will how u trace performance with System.Diagnostics.Trace. Personnally i do the following
DateTime D1 = DateTime.Now ; //in the beginning of the procedure
/……….
DateTime D2 = DateTime.Now ; //in the end of the procedure
Console.WriteLine ((D2-D1).ToString()); And I Obtain the time spent in the procedure.



GeneralRe: improvements Pin
Alexei Tarnakin24-Jun-05 9:35
Alexei Tarnakin24-Jun-05 9:35 
GeneralRe: improvements Pin
Hayder Marzouk29-Jun-05 9:07
Hayder Marzouk29-Jun-05 9:07 
GeneralRe: improvements Pin
Alexei Tarnakin1-Jul-05 2:23
Alexei Tarnakin1-Jul-05 2:23 
GeneralRe: improvements Pin
Hayder Marzouk2-Jul-05 10:14
Hayder Marzouk2-Jul-05 10:14 
GeneralRe: improvements Pin
Alexei Tarnakin3-Jul-05 4:27
Alexei Tarnakin3-Jul-05 4:27 
GeneralRe:Improvements Pin
Hayder Marzouk3-Jul-05 10:43
Hayder Marzouk3-Jul-05 10:43 
GeneralRe:Improvements Pin
Alexei Tarnakin5-Jul-05 4:41
Alexei Tarnakin5-Jul-05 4:41 
GeneralRe:Improvements Pin
Alexei Tarnakin5-Jul-05 5:07
Alexei Tarnakin5-Jul-05 5:07 
GeneralRe:Improvements Pin
Hayder Marzouk5-Jul-05 21:47
Hayder Marzouk5-Jul-05 21:47 
GeneralRe:Improvements Pin
Hayder Marzouk5-Jul-05 21:56
Hayder Marzouk5-Jul-05 21:56 
GeneralRe:Improvements Pin
Hayder Marzouk5-Jul-05 21:52
Hayder Marzouk5-Jul-05 21:52 
GeneralRe:Improvements Pin
Alexei Tarnakin5-Jul-05 23:30
Alexei Tarnakin5-Jul-05 23:30 
GeneralRe:Improvements Pin
Alexei Tarnakin6-Jul-05 4:12
Alexei Tarnakin6-Jul-05 4:12 
GeneralRejectChanges method to BaseList object Pin
Johny Nguyen15-Jun-05 22:45
Johny Nguyen15-Jun-05 22:45 
GeneralRe: RejectChanges method to BaseList object Pin
Hayder Marzouk16-Jun-05 0:16
Hayder Marzouk16-Jun-05 0:16 
GeneralRe: RejectChanges method to BaseList object Pin
Johny Nguyen16-Jun-05 18:06
Johny Nguyen16-Jun-05 18:06 
GeneralHowto Implement IEditableObject method Pin
Johny Nguyen6-Jun-05 18:59
Johny Nguyen6-Jun-05 18:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.