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

Datagridview with filtering capability

Rate me:
Please Sign up or sign in to vote.
3.55/5 (21 votes)
20 Jun 2007CPOL2 min read 139.8K   11.2K   65   28
Datagridview with filtering capability

Introduction

The Microsoft Windows Forms Datagridview provided with the MS Visual Studio 2005 has got a lot new and good features which were missing in the datagrid control provided in the earlier versions of MS .Net. But still, many times the developers or I should say the functional need of the application require the filtering capabilities.

In this article, I am gonna explain the method by which we can add the filtering capabilities in the Datagridview header itself.

To just start with and summarize, it can be entirely customized through an xml file and decided to which of columns, the filtering capability is to be provided.

Unlike most of the filtering grids available, it allows you to apply filter on more than one filter at a time and in that too, you can select multiple values in a column. Thus extending the feature for searching/filtering or we should say minimizing the desired view of data to the bare minimum level. Screenshot - 1.jpg

Screenshot - 2.jpg

Using the code

FilterDataGridView is a fully customizable user control. As far as the developer's point of view is concerned. He will not have to make a single line of code change in his code and yet he can achieve the filtering capability. J

As many of the times, we don't want to give the filtering capability to all the columns that why FilterDataGridView reads the name of the columns from an xml file which have to be enabled for the filtering.

That means what all one developer has to do is to provide an xml file with the name of the columns to be filtered.

Below, I have mentioned what all steps would need to be taken if you want to replace the old Datagridview with the FilterDataGridView.

  1. Add a reference to the FilteredGridViewControl.dll in your project and also add it to your toolbar.
  2. Drag the FilteredGridView from your toolbar to the window form as you do with any other control.
  3. Add FilterGridView.xml to your application. In the compile property, set the Copy to Output folder option.

This xml should look like this:

<?xml version="1.0" encoding="utf-8" ?> 
<ColumnsToBeFiltered> 
<ColumnName>Branch</ColumnName> 
<ColumnName>City</ColumnName> 
</ColumnsToBeFiltered> 

You can specify as many columns here which are to be filtered.

And here we go, all is set. Wasn't that simple?

How it works?

With FilteredGridView, when you right click on a header of a column which is there in the list of Columns to be filtered, it will list all the distinct rows. Now you can select any of the row it your grid will automatically be filtered with that value. FilteredGridView supports applying multiple filters on a single column. That means you can apply as many filters on a column and at the same time you can narrow the data by putting filters on other columns data too.

Assumptions

This version of FilterGridView only works when bound with a Data Source.

License

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


Written By
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


Comments and Discussions

 
Questiondownload not working Pin
Member 139440729-Aug-18 19:24
Member 139440729-Aug-18 19:24 
QuestionDropdown columns Pin
sridharsr23-Aug-14 6:25
sridharsr23-Aug-14 6:25 
Questionnot sure what I'm doing wrong Pin
esb7718-Jun-13 9:55
esb7718-Jun-13 9:55 
GeneralMy vote of 5 Pin
Digital Geek21-Nov-12 23:36
Digital Geek21-Nov-12 23:36 
GeneralRe: My vote of 5 Pin
Manas Bhardwaj21-Nov-12 23:43
professionalManas Bhardwaj21-Nov-12 23:43 
GeneralRe: My vote of 5 Pin
Digital Geek22-Nov-12 2:16
Digital Geek22-Nov-12 2:16 
QuestionA Good Solution also Pin
code-kings10-Oct-12 6:38
code-kings10-Oct-12 6:38 
Questionthere is a problem with VS 2010 Pin
esb776-Oct-12 14:20
esb776-Oct-12 14:20 
AnswerRe: there is a problem with VS 2010 Pin
esb777-Oct-12 5:59
esb777-Oct-12 5:59 
GeneralRe: there is a problem with VS 2010 Pin
Manas Bhardwaj7-Oct-12 6:42
professionalManas Bhardwaj7-Oct-12 6:42 
Questionmulti select filter Pin
arkiboys1-Feb-12 23:53
arkiboys1-Feb-12 23:53 
QuestionDatagridview filtering.dll Pin
Maazatron14-Dec-11 3:01
Maazatron14-Dec-11 3:01 
AnswerRe: Datagridview filtering.dll Pin
arkiboys1-Feb-12 23:56
arkiboys1-Feb-12 23:56 
GeneralCountering thr rows Pin
Member 299000828-Aug-10 10:18
Member 299000828-Aug-10 10:18 
GeneralDynamic DataGridView filtering in C# Pin
Ivan (Zaragoza)3-Dec-08 22:50
Ivan (Zaragoza)3-Dec-08 22:50 
GeneralThis is a project Similar to MSDN: http://msdn.microsoft.com/en-us/library/aa480727.aspx Pin
TamusRoyce13-Nov-08 10:29
TamusRoyce13-Nov-08 10:29 
GeneralI am getting an error "Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'." Pin
bondonian29-Sep-08 9:34
bondonian29-Sep-08 9:34 
GeneralI did some changes. post here to anyone interested Pin
zhngmm26-Aug-08 13:24
zhngmm26-Aug-08 13:24 
GeneralRe: I did some changes. post here to anyone interested Pin
bondonian29-Sep-08 11:15
bondonian29-Sep-08 11:15 
GeneralRe: I did some changes. post here to anyone interested Pin
zhngmm29-Sep-08 17:23
zhngmm29-Sep-08 17:23 
GeneralNice work Pin
Mohammad Dayyan16-Aug-08 3:56
Mohammad Dayyan16-Aug-08 3:56 
GeneralUse it in more than one datagridview Pin
davomoreda6-May-08 0:08
davomoreda6-May-08 0:08 
GeneralLooks Good but... Pin
Pat Tormey3-Jul-07 1:07
Pat Tormey3-Jul-07 1:07 
GeneralDoe not work when using without data source. Pin
Joe Sonderegger15-Jun-07 1:39
Joe Sonderegger15-Jun-07 1:39 
GeneralCant add it to my page Pin
DumtySoft14-Jun-07 8:36
DumtySoft14-Jun-07 8:36 

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.