Skip to main content

Searching Inside the GridView Control

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HeaderSearch.aspx.cs" Inherits="HeaderSearch" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <asp:GridView ID="GridView1" AllowPaging="true" runat="Server" AutoGenerateColumns="False"
               OnRowCreated="GridView1_RowCreated">
               <Columns>
                   <asp:TemplateField>
                       <HeaderTemplate>
                       <label>Enter Search String</label>
                           <asp:TextBox ID="txtSearch" runat="Server">
                           </asp:TextBox>
                       </HeaderTemplate>
                       <ItemTemplate>
                           <asp:Label ID="lblIsbn" runat="Server" Text='<%#Eval("ISBN")%>'></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:Label ID="lblTitle" runat="Server" Text='<%#Eval("Title") %>'></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField>
                       <HeaderTemplate>
                           <asp:Button ID="btnSearch" runat="Server" Text="Search" />
                       </HeaderTemplate>
                       <ItemTemplate>
                           <asp:Label ID="lblPublisher" runat="Server" Text='<%#Eval("Publisher") %>'></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
           </asp:GridView>
       </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class HeaderSearch : System.Web.UI.Page
{
    protected string searchString=string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridViewView();
        }
    }
    private void BindGridViewView()
    {
        if (Session["strTemp"] != null)
        {

            GridView1.DataSource = Session["strTemp"] as DataTable;
            GridView1.DataBind();

        }
        else
        {
            GridView1.DataSource = GetCustomMadeDataTable();
            GridView1.DataBind();
        }
    }
    public DataTable GetCustomMadeDataTable()
    {
        //Create a new DataTable object
        System.Data.DataTable objDataTable = new System.Data.DataTable();
        //Create three columns with string as their type
        objDataTable.Columns.Add("ISBN", typeof(string));
        objDataTable.Columns.Add("Title", typeof(string));
        objDataTable.Columns.Add("Publisher", typeof(string));
        objDataTable.Columns.Add("Year", typeof(string));
        DataColumn[] dcPk = new DataColumn[1];
        dcPk[0] = objDataTable.Columns["ISBN"];
        objDataTable.PrimaryKey = dcPk;
        objDataTable.Columns["ISBN"].AutoIncrement = true;
        objDataTable.Columns["ISBN"].AutoIncrementSeed = 1;
        //Adding some data in the rows of this DataTable
        DataRow dr;
        for (int i = 1; i <= 5; i++)
        {
            dr = objDataTable.NewRow();
            dr[1] = "Title" + i.ToString();
            dr[2] = "Publisher" + i.ToString();
            dr[3] = "200" + i.ToString();
            objDataTable.Rows.Add(dr);
        }
        for (int i = 6; i <= 8; i++)
        {
            dr = objDataTable.NewRow();
            dr[1] = "Computer" + i.ToString();
            dr[2] = "TMH" + i.ToString();
            dr[3] = "200" + i.ToString();
            objDataTable.Rows.Add(dr);
        }
        Session["strTemp"] = objDataTable;
        return objDataTable;
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            SetHeader(GridView1, e.Row, this);
        }
       
          
       

    }
   
    public void SetHeader(GridView gridView, GridViewRow gvHeaderRow, Page page)
    {

        TextBox txtSearch = (TextBox)gvHeaderRow.FindControl("txtSearch");
        Button btnSearch = (Button)gvHeaderRow.FindControl("btnSearch");
        btnSearch.Click += delegate
            {

                DataTable dt = Session["strTemp"] as DataTable;
                DataView dv = dt.DefaultView;
                dv.RowFilter = "title like'%" + txtSearch.Text + "%'";
                GridView1.DataSource = dv;
                GridView1.DataBind();

            };


    }
  

}

Comments

Popular posts from this blog