Click here to Skip to main content
13,097,609 members (53,699 online)
Click here to Skip to main content
Add your own
alternative version


78 bookmarked
Posted 4 Mar 2005

DataGrid Filter

, 10 Mar 2005
Rate this:
Please Sign up or sign in to vote.
How to customize DataGrid columns and filter data.


This article shows how to filter data with the RowFilter property and change the column appearance using C# objects.

I wrote this article after I searched for existing code that enables us to customize the DataGrid object. The demands were to enable the "End User" to filter the data associated with the DataGrid and choose the DataGrid columns which are shown on the grid. I found that .NET objects (DataView, DataGridTableStyle) already supported the filter idea and I needed to make only a few adjustments.

Column Filter

The DataGridTableStyle object enables us to map columns with the MappingName property. A column that does not belong to the DataGrid table style mapping will not be shown on the grid. The "SetTableBySelectedCoulmns" function gets the user's column selection and builds dynamically the "DataGridTableStyle". The DataGrid column style supports two column types: "bool" and "text". If the column data type is "System.Boolean" the function sets new "DataGridBoolColumn" otherwise the default option sets new "DataGridTextBoxColumn".

DataGridTableStyle GridStyle = new DataGridTableStyle();
DataGridColumnStyle   TextBoxStyle; //Use for System.Boolean only
DataGridColumnStyle   BoolStyle; //Use for all Data Types 
                                 //which different form System.Boolean

string CoulmnDataType;  // hold the column Data Type 
     //clear the previous Table Styles
     GridStyle.MappingName = TableMappingName;

     foreach (DataColumn Column in TableColumnCollection )
         CoulmnDataType = Column.DataType.ToString();

         // The "CheckedColumns" contains the 
         // column which the user select to show 
         // Column that not belong to the mapping 
         // will not show on the grid
         if (CheckedColumns.CheckedItems.Contains(Column.ColumnName))
             switch (CoulmnDataType)
                 // The DataGrid Coulmn Style support two 
                 // major column types: Bool and Text 
                 case ("System.Boolean"):
                    BoolStyle = new DataGridBoolColumn ();
                    BoolStyle.Width=100 ;

                     TextBoxStyle= new DataGridTextBoxColumn();
                     // The NUllText attribute enable to replace the default null 
                     // value for empty cells 
                     TextBoxStyle.NullText = string.Empty ;

     //Set the Grid Style & Color
     GridStyle.AlternatingBackColor=System.Drawing.Color.AliceBlue ;
     GridStyle.GridLineColor = System.Drawing.Color.MediumSlateBlue;

Default column

If you want to "remember" the user column selection the next time you use the program, then you can save the "TableColumnCollection" table in XML which can be easily read into the table each time the program is loaded.

Data Filter:

The Data Filter section uses the RowFilter object. The filter function builds the RowFilter statement according to the user restriction. The function uses the table associated with the DataFilter grid. Each row consists of:

  • Column Name
  • Operation
  • Data to compare

The main loop checks if the user adds any restriction to the current column, if so the restriction is added to the Row Filter statement.

ViewRecords = new DataView( DataSetRecords.Tables[0]);

    //Build the RowFilter statement according to the user restriction
    foreach (DataRow FilterRow in TableFilterData.Rows)
        if (FilterRow["Operation"].ToString() != 
          string.Empty && FilterRow["ColumnData"].ToString() != string.Empty)
            // Add the "AND" operator only from the 
            // second filter condition 
            // The RowFilter get statement which simallar 
            // to the Where condition in sql query
            // For example "GroupID = '6' AND GroupName LIKE 'A%' 

            if (ViewRecords.RowFilter == string.Empty)
                ViewRecords.RowFilter = FilterRow["ColumnName"].ToString() + " " + 
                             FilterRow["Operation"].ToString() + " '" + 
                                FilterRow["ColumnData"].ToString()+"' ";
                 ViewRecords.RowFilter += " AND " + 
                               FilterRow["ColumnName"].ToString()+" " + 
                               FilterRow["Operation"].ToString() +" '"+ 

The DataGrid ComboBox was downloaded from Jan Wiggers article (thanks Jan): A ComboBox in a DataGrid


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


About the Author

Aviram Solomon
Product Manager TTI-Telecom
Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions code Pin
Sachin Bansode9-Jun-10 22:15
memberSachin Bansode9-Jun-10 22:15 
Questioncan i use it in VB.Net windows based application Pin
Sridhar CS7-Apr-06 17:54
memberSridhar CS7-Apr-06 17:54 
Questioncan i use it with Pin
Sumaira Butt5-Oct-05 20:20
memberSumaira Butt5-Oct-05 20:20 
AnswerRe: can i use it with Pin
raybristol4-Mar-06 14:28
memberraybristol4-Mar-06 14:28 
GeneralIndeterminate State of Check Box column Pin
Mukund Pujari3-Oct-05 18:16
memberMukund Pujari3-Oct-05 18:16 
GeneralThank you, Aviram Solomon Pin
phunqv6-Jul-05 16:11
memberphunqv6-Jul-05 16:11 
GeneralDatagrid Filter Component with Textboxes Pin
peitor14-Mar-05 22:52
memberpeitor14-Mar-05 22:52 
Questionmany filter? Pin
dxhdxh10-Mar-05 15:45
memberdxhdxh10-Mar-05 15:45 
AnswerRe: many filter? Pin
Aviram Solomon11-Mar-05 0:15
memberAviram Solomon11-Mar-05 0:15 
QuestionWhere are the project files? Pin
bluish9-Mar-05 15:52
memberbluish9-Mar-05 15:52 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 10 Mar 2005
Article Copyright 2005 by Aviram Solomon
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid