Filter the Data on GridView

Introduction

This article explains how to filter the data in a GridView.

Step 1

I first created a database with a few entries of some IT students. I named this database "Student", in this database I created a table named IT_Students. It's data is as follows:

filter data on grid

Step 2

After this I created a new application in Visual Studio.

On this application I took a TextBox, a button and a Grid View as in the following:

  1. <div>  
  2.   Search User by Name:-  
  3.    <asp:TextBox runat="server" ID="txt1"></asp:TextBox>  
  4.   <br />  
  5.     <asp:Button ID="btn1" runat="server" Text="Submit" OnClick="btn1_Click" />  
  6. </div>  
  7.   <br />  
  8. <div>  
  9.    <asp:Label ID="lbl1" runat="server" Enabled="false"></asp:Label>  
  10.        <asp:GridView ID="grd" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
  11.          <AlternatingRowStyle BackColor="White" />  
  12.          <EditRowStyle BackColor="#2461BF" />  
  13.          <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  14.          <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  15.          <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
  16.          <RowStyle BackColor="#EFF3FB" />  
  17.          <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />  
  18.          <SortedAscendingCellStyle BackColor="#F5F7FB" />  
  19.          <SortedAscendingHeaderStyle BackColor="#6D95E1" />  
  20.          <SortedDescendingCellStyle BackColor="#E9EBEF" />  
  21.          <SortedDescendingHeaderStyle BackColor="#4870BE" />  
  22.     </asp:GridView>  
  23. </div> 

Step 3

After this you need to bind the database to the GridView, on running the application the user will see all the data in the grid, for this I have provided the binding on the page load event.

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     x = new SqlConnection(@"Data Source=MCNDESKTOP20\MCNDESKTOP20;  
  4.     Initial Catalog=Student;Integrated Security=True");  
  5.     x.Open();  
  6.     SqlDataAdapter da = new SqlDataAdapter("select * from IT_Student", x);  
  7.     DataSet ds = new DataSet();  
  8.     da.Fill(ds);  
  9.     grd.DataSource = ds;  
  10.     grd.DataBind();  
  11. }

You can see that I had simply bound the GridView with the Database, so on running the application the GridView will show all the data available in the database.

Step 4

Now I will work on the button click event.

  1. protected void btn1_Click(object sender, EventArgs e)  
  2. {  
  3.     x = new SqlConnection(@"Data Source=MCNDESKTOP20\MCNDESKTOP20;  
  4.         Initial Catalog=Student;Integrated Security=True");  
  5.     x.Open();  
  6.     SqlDataAdapter da = new SqlDataAdapter("select * from IT_Student where   
  7.         Student_Name like '%" + txt1.Text + "%'", x);  
  8.     DataSet ds = new DataSet();  
  9.     da.Fill(ds);  
  10.     grd.DataSource = ds;  
  11.     grd.DataBind();  
  12. } 

Here I had applied the filtering on SQL data, whatever the text is provided in the TextBox that will be used to filter the name of students, it will check the names from the start to the end and will provide all the names with the similar text.

Now our application is created and is ready for it's execution.

Output

On running the application you will see that the Grid has all the data available in the database.

filter data on grid

Now I will enter some text into the TextBox and then click on the button.

filter data on grid

You can see that all the names with the "an", whether at the start, in the middle or at the end, are available in the Grid.