Click here to Skip to main content
13,799,874 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


28 bookmarked
Posted 16 Apr 2010
Licenced CPOL

Gridview Header Filter

, 22 Apr 2010
Rate this:
Please Sign up or sign in to vote.
To help you filter gridview using its header


Most of the time, we need to filter gridview based on some filtering criteria. This article will show you how to filter using the gridview header. This is small but really useful stuff. You can extend it to suite your needs.


Following are the prerequisites for this project:

  1. VS 2008 with Service Pack 1
  2. Ajax control toolkit DLL

Using the Code

The code is extremely simple and easy to use.

Define a HeaderTemplate for the column you want to filter on. Simply add a textbox with borderstyle set to None, AutoPostBack set to true and BackColor set to transparent. This will give a feel of label. Create a TextBoxWaterMarkExtender with TargetControlID set to the id of previous textbox. Set the WaterMarktext to the Header text you want to display.

<asp:TextBox ID="txtCategoryNameHeader" runat="server" 

BorderStyle="None" AutoPostBack="true" BackColor="Transparent" 

OnTextChanged="txtCategoryNameHeader_TextChanged" ></asp:TextBox>
<asp:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" 

TargetControlID="txtCategoryNameHeader" WatermarkText="Category">

Once you set the HTML tag, you can code it. You need to bind the txtCategoryNameHeader_TextChanged event with the following line of code:

((TextBox)this.gvCategory.HeaderRow.Cells[1].Controls[1]).TextChanged += 
						new System.EventHandler


Once you enter some text inside the header textbox, a post back event will fire and the following method will be executed:

protected void txtCategoryNameHeader_TextChanged(object sender, EventArgs e)
            string text = ((TextBox)sender).Text;

Rest of the filtering criteria is pretty self explanatory, so I wouldn't further explain it. Hope this will help you guys.

protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                // Bind the categories with Grid
                // This is used to bind the textbox header with the event to fire when 
					+= new System.EventHandler

        private void BindCategories(string CategoryFilter)
                // Simple created a table to bind with Grid view and 
                // populated it with data.
                DataTable dt = new DataTable("Category");
                DataRow dr ;
                for(int counter=1;counter<11;counter++)
                    dr = dt.NewRow();
                    dr["Name"]= "Cat" + counter.ToString();

                DataView dv = new DataView(dt);
                if(CategoryFilter != "")
                    dv.RowFilter="Name like '%" + CategoryFilter + "%'";

                if (CategoryFilter == "")
                    gvCategory.DataSource = dv;
                    gvCategory.DataSource = dv;
            catch (Exception ex)

        protected void txtCategoryNameHeader_TextChanged(object sender, EventArgs e)
            string text = ((TextBox)sender).Text;

Points of Interest

There do exist several ways to manipulate gridview and I found this one interesting so I just published it. For daily technical stuff, checkout my blog at


  • 16th April, 2010: First version published


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


About the Author

Wahab Hussain
Web Developer
Pakistan Pakistan
Follow my blogging activities here:-

Follow my professional activities here:-

Follow my tweets here:-!/hellowahab

Follow my personal activities here:-

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
diogonborges7-Jan-13 6:08
memberdiogonborges7-Jan-13 6:08 
QuestionGridView disappears Pin
Homey8323-Nov-11 12:16
memberHomey8323-Nov-11 12:16 
Questionvariation Pin
Sean80848-Sep-11 8:30
memberSean80848-Sep-11 8:30 
GeneralMy 5 Pin
Omar Gameel Salem1-Jun-11 5:34
professionalOmar Gameel Salem1-Jun-11 5:34 
GeneralMy vote of 5 Pin
Member 41788502-Jul-10 4:05
memberMember 41788502-Jul-10 4:05 
GeneralUsing < > = in Pin
JanBorup3-May-10 12:20
memberJanBorup3-May-10 12:20 

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 | Cookies | Terms of Use | Mobile
Web04 | 2.8.181214.1 | Last Updated 23 Apr 2010
Article Copyright 2010 by Wahab Hussain
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid