![]() |
Desktop Development »
Miscellaneous »
General
Intermediate
License: The Code Project Open License (CPOL)
An Array Data Grid with sort and filter abilityBy beatles1692it's a custom data grid that gets an array object as its data source and you can sort and filter it |
C# 1.0, Windows, .NET 1.1VS.NET2003, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

System.Windows.Forms.DataGrid is a great control with so many features but i think that it's a little bit hard to use it. So I wrote a user control that uses a data grid but it's a little bit easier to use. Here are its features:
you can set its data source ,like this:
myGrid.DataSource=users; //users is an array of type User.
You can format the grid using a GridFormatter object.
A grid formatter is an object that has some column formatter that will tell the grid how to format itself.Each column formatter contains a DataGridColumnStyle type.
GridFormatter format1=new GridFormatter() ;
format1.AddColumnFormatter("FirstName","FirstName",typeof(DataGridTextBoxColumn));
If you want to hide a column just don't add a column formatter to your grid formatter.
When grid formatter is ready we set our data grid formatter to let it format itself.
myGrid.GridFormatter=format1;
The good news is that you can change your formatter anytime you like.
Hint:in order to format an empty grid you can pass an empty array of your object to its datasource and then format it .
Hint:Since GridFormatter is using a DataGridColumnStyle object to format the grid you can easily show almost every thing in you grid.(Here is an article about DataGridColumnStyle by Declan Brennan that I found very interesting).
There are times that you want to add or remove records at run time.
You can do it using AddRow(object obj) and AddRows(object[] objects) methods.
myGrid.AddRow(new User("FName","LName"));
And if you want to remove some records use Remove(object obj) or Remove(object[] objects)
You can get the current row (as an object) using SelectedItem property
User selectedUser=(User)myGrid.SelectedItem;
You can get selected items (as an array of object) using SelectedItems property
usersGrid.AddRows((User[])myGrid.SelectedItems);
You can apply a filter to your grid using ApplyFilter(ExpressionBuilder expression).
Apply filter takes an expression builder to build its filter expression.An expression builder is an object that builds an string.
For example if you like to say "FirstName='Fname' And Age>'15'" you will write:
ExpressionBuilder expBuilder=new AndExpression(new EqExpression("FirstName","FName"),new GtExpression("Age",15));
It is very similar to hibernate/nhibernate expression object model.
there are different expression builders that work together using a composite pattern therefore you can combie them to make a complex expression.
Now we have an expression builer we can filter our grid.
myGrid.ApplyFilter(expBuilder);
To remove filter simply use RemoveFilter() method.
Hint:If you filter a data grid and add an object that doesn't fit in applied filter condition ,it will be added but it will be hidden until you remove the filter
If you want to sort a datagrid you can use an array of Order object(again it's very similar to hibernate/NHibernate).
An order object takes a column name and a bool value indicating to sort ascending(true) or descending(false)
Order[] order=new Order[]{new Order("FirstName",true),new Order("LastName",false)};
myGrid.Sort(order);
This is my firt article at codeproject.I hope you find it useful and please let me know your opinion and comments.
Let me know what other features this grid should have.
Thank you
Nima H
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 19 Oct 2006 Editor: |
Copyright 2006 by beatles1692 Everything else Copyright © CodeProject, 1999-2009 Web09 | Advertise on the Code Project |