SIGN UP MEMBER LOGIN:    
ARTICLE

Quick way to Filter data in GridView in ASP.NET SQL Server DB

Posted by Abhimanyu Kumar Vatsa Articles | ASP.NET Programming October 08, 2011
In this post you will learn how to filter data (records) and display it in GridView Control quickly.
Reader Level:
Download Files:
 

Introduction

In this post we are going to cover following points:

  • Binding tabular data to a GridView control using the SqlDataSource control
  • Filtering the data using quick method
  • Focus on imperative code rather than declarative code

I am going to explain it using step by step method so that you can follow it and create the sample application. Follow the steps:

Step 1:

Create the database as its structure given below in screenshot.

image002.jpg

Step 2:

Now drag the database table from Server Explorer to form (which should be in design mode), find the screenshot given below.

image004.jpg

Step 3:

Now add a DropDownList box and list some item in this. For this you need to create a DropDownList on form in design mode and then select its Items properties this will pop a window which will allow you to add the items for this. You can simply add the items using code as given below.

        <asp:DropDownList ID="DropDownList1" runat="server">
           
<asp:ListItem>name</asp:ListItem>
           
<asp:ListItem>address</asp:ListItem>
           
<asp:ListItem>mobile</asp:ListItem>
       
</asp:DropDownList>

image006.jpg

Step 4

When you done with DropDownList, you need to add a TextBox that will accept the textual data to filter records and also you need to add a Button to perform filter task by its click event.

image008.jpg

Step 5

Now our design work is over, let's place some code in button click event to filter records. Remember I already have place some records in my database table. Here is the code.

    protected void Button1_Click(object sender, EventArgs e)
   
{
       
string FilterExpression = string.Concat(DropDownList1.SelectedValue, " LIKE '%{0}%'");
       
SqlDataSource1.FilterParameters.Clear();
       
SqlDataSource1.FilterParameters.Add(new ControlParameter(DropDownList1.SelectedValue, "TextBox1", "Text"));
       
SqlDataSource1.FilterExpression = FilterExpression;
   
}

When I run the application, I have following records.

image010.jpg

Let's filter it by name or address or mobile, I am trying filter records by name by typing just 'o' in text box. It will filter all names contain character 'o'. Here is my output.

image012.jpg

In the same way, you can try by address or mobile as well.

I hope you like it. Comments Please.

Login to add your contents and source code to this article
share this article :
post comment
 

i am waiting bro just tell me , i am stuck

Posted by abdul muqeet May 23, 2012

bro just tell what should i do for Int , i will do it , just give me the ides

Posted by abdul muqeet May 23, 2012

bro can you please show me how to do it for Int.

Posted by abdul muqeet May 22, 2012

please give me solution , i would be very help full for me , i am new i serching every thing on net i have to get experience ,,.. and also if my grid view is connected in inner join will it work same as its working with one table??? can you please show me how can i filter Int or numeric in my grid view...

Posted by abdul muqeet May 22, 2012

@abdul: in this example i've all fiend in varchar type. you need to go for some workaround.

Posted by Abhimanyu Kumar Vatsa May 22, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor