Skip Navigation LinksHome > Categories > Code from a Category

How to Create Microsoft Excel's AutoFilter in ASP.Net



User Name: codelecturer
Name: Mike Chauhan
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 3551
Add Date: 10/11/2012
Here I am explaining how to build a feature similar to Microsoft’s Excel AutoFilter in ASP.Net GridView control.
Excel AutoFilter allows user to filter the records using the DropDownList in the header, so lets start with the tutorial

GridView Markup



In this tutorial I’ll be explaining the AutoFilter feature using the Country Field of the Customers table hence you’ll notice I have used a Template field for the Country column in GridView with a DropDownList in the header template which will be used to Filter the records

Database
For this tutorial I am using Microsoft’s NorthWind Database. You can download it using the link below
Download NorthWind Database

Stored Procedure
I’ll be using stored procedure to get the Customer records from the Customers table of NorthWind Database.



You will notice above the stored procedure accepts a parameter called @Filter. Based on the value of this parameter the stored procedure filters the records

Binding the GridView

C#


VB.Net


You will notice above, I am calling the same store procedure I created earlier and passing the Filter criteria as parameter to it. Also I am calling a method BindCountryList that will be used to populate my AutoFilter Country DropDownList

Populating AutoFilter DropDownList
The following method is used to populate the AutoFilter DropDownList which present in the Header Template of the ASP.Net GridView control

C#


VB.Net


The above method simply fires a query on the Customers table to get distinct country names present in the table

AutoFilter Functionality
The AutoFilter functionality is manages by the Country DropDownList’s OnSelectedIndexChanged event.

C#


VB.Net


Here I am simply setting the filter value from the DropDownList’s selected item’s value into the ViewState variable Thus when the DropDownList’s selected value is changed this event is fired and the GridView is rebind with filtered records

Page Load Event
Here is the page load event of the ASP.net Web Page it is simply used to bind the Grid when the page loads for first time

C#


VB.Net


PageIndexChaging event
And here is the OnPageIndexChaging Event

C#


VB.Net




Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.