Search ASP.NET GridView Using C#

Here is a code example of searching a GridView control in ASP.NET. 
 
Create an ASP.NET Web Application using Visual Studio.
 
Step 1: Download ziped Bin folder and copy it to your Bin folder
 
Step 2: Add dll to your toolbox and drag it to your web page.
 
Step 3: Now, change the default aspx page. I add some controls to the page. See below.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <%@ Register assembly="SamApp.WebControls.SearchGridView" namespace="SamApp.WebControls" tagprefix="cc1" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head id="Head1" runat="server">  
  6. <title></title>  
  7. </head>  
  8. <body>  
  9. <p>  
  10. <br />  
  11. </p>  
  12. <form id="form1" runat="server">  
  13. <div style="height: 100px">  
  14. <asp:HiddenField ID="hfSearchText" runat="server" />  
  15. <asp:HiddenField ID="hfSort" runat="server" />  
  16. </div>  
  17. <div>  
  18. <cc1:SearchGridView ID="SearchGridView1" runat="server" BackColor="White"  
  19. BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"  
  20. EnableModelValidation="True"  
  21. onpageindexchanging="SearchGridView1_PageIndexChanging"  
  22. onsearchgrid="SearchGridView1_SearchGrid" ShowFooter="True"  
  23. AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"  
  24. onsorting="SearchGridView1_Sorting" ForeColor="Black" GridLines="Vertical">  
  25. <SearchFilters>  
  26. <asp:ListItem>Username</asp:ListItem>  
  27. <asp:ListItem Value="User_Pass">Password</asp:ListItem>  
  28. <asp:ListItem Value="F_Name">First Name</asp:ListItem>  
  29. <asp:ListItem Value="L_Name">Last Name</asp:ListItem>  
  30. <asp:ListItem Value="City">City</asp:ListItem>  
  31. </SearchFilters>  
  32. <AlternatingRowStyle BackColor="#CCCCCC" />  
  33. <Columns>  
  34. <asp:BoundField DataField="Username" HeaderText="Username"  
  35. SortExpression="Username" />  
  36. <asp:BoundField DataField="User_Pass" HeaderText="Password"  
  37. SortExpression="User_Pass" />  
  38. <asp:BoundField DataField="F_Name" HeaderText="First Name"  
  39. SortExpression="F_Name" />  
  40. <asp:BoundField DataField="L_Name" HeaderText="Last Name"  
  41. SortExpression="L_Name" />  
  42. <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />  
  43. </Columns>  
  44. <FooterStyle BackColor="#CCCCCC" />  
  45. <HeaderStyle BackColor="#333399" Font-Bold="True" ForeColor="Silver" />  
  46. <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />  
  47. <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />  
  48. </cc1:SearchGridView>  
  49. <br />  
  50. <br />  
  51. </div>  
  52. <div>  
  53. </div>  
  54. </form>  
  55. </body>  
  56. </html>  
Step 4: Code behind
Here is the code behind. In this code, I load data from a database table User_Login. Make sure you need to change your database connection string, SELECT statement based on your database table.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. public partial class _Default : System.Web.UI.Page  
  10. {  
  11. public static SqlConnection conn = new SqlConnection(@"Your connection String!!!");  
  12. protected void Page_Load(object sender, EventArgs e)  
  13. {  
  14. if (!Page.IsPostBack)  
  15. {  
  16. bind();  
  17. }  
  18. }  
  19. public void bind()  
  20. {  
  21. SearchGridView1.DataSource = GetTable();  
  22. SearchGridView1.DataBind();  
  23. }  
  24. public DataView GetTable()  
  25. {  
  26. SqlDataSource ds = new SqlDataSource();  
  27. ds.ConnectionString = @"server=IT-WSPC-F10\SQL;User Id=sa;Password=inveera@123;Initial Catalog=pankaj";  
  28. ds.SelectCommand = "select * from User_Login";  
  29. if (hfSearchText.Value != "")  
  30. ds.SelectCommand += " where " + hfSearchText.Value;  
  31. DataView dv = (DataView)ds.Select(new DataSourceSelectArguments());  
  32. if (hfSort.Value != "")  
  33. dv.Sort = hfSort.Value;  
  34. return dv;  
  35. }  
  36. protected void SearchGridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  37. {  
  38. SearchGridView1.PageIndex = e.NewPageIndex;  
  39. bind();  
  40. }  
  41. protected void SearchGridView1_SearchGrid(string _strSearch)  
  42. {  
  43. hfSearchText.Value = _strSearch;  
  44. bind();  
  45. }  
  46. protected void SearchGridView1_Sorting(object sender, GridViewSortEventArgs e)  
  47. {  
  48. if (hfSort.Value == e.SortExpression)  
  49. hfSort.Value = e.SortExpression + " Desc";  
  50. else  
  51. hfSort.Value = e.SortExpression;  
  52. bind();  
  53. }  
  54. }