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
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
DataView features (Sorting, Filtering and the
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.
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,...).
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 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.
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
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
private string TmpName;
public string Name
if (base.Compare(TmpName, value) !=0)
TmpName = value;
Then create the
Customers class which inherit from abstract class
CustomCollection. Code will be like this:
public class Customers: CustomCollection
this.ItemType = typeof(Customer);
public new Customer this[int index]
base[index] = value;
public new Customers GetChanges()
return (Customers) base.GetChanges();
The demo project includes also source code of
CustomCollection, no need to work with
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.