Click here to Skip to main content
6,594,432 members and growing! (15,770 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate License: The Code Project Open License (CPOL)

An Array Data Grid with sort and filter ability

By beatles1692

it'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
Posted:19 Oct 2006
Views:20,856
Bookmarked:18 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 1.02 Rating: 3.40 out of 5

1

2
1 vote, 50.0%
3

4
1 vote, 50.0%
5

Sample Image - Figure1.jpg

Introduction

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:

  • It gets an array of objects as its data source
  • It returns its selected item as an object
  • It returns its selected items as an array of objects
  • You can format it very easily
  • You can add an object to its datasource
  • You can remove an object from its datasource
  • You can filter its datasource
  • You can sort its datasource

How it works

Setting Data Source

you can set its data source ,like this:

myGrid.DataSource=users; //users is an array of type User.

Formatting:

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).

Adding and removing records:

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)

Getting selected items:

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);

Filtering and sorting:

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);

Final word:

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

See also:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

beatles1692


Member

Occupation: Web Developer
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

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

PermaLink | Privacy | Terms of Use
Last Updated: 19 Oct 2006
Editor:
Copyright 2006 by beatles1692
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project