Please refer this example
Ref:Search Filter GridView Records based on TextBox in ASP.Net
HTML
<form id="form1" runat="server">
<div>
Id:
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" OnClick = "Search" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" OnPageIndexChanging="GridView1_OnPageIndexChanging" PageSize="3" AllowPaging="true">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Contact Id" />
<asp:BoundField DataField="ContactName" HeaderText="Contact Id" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</div>
</form>
Namespace
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void Search(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string query = "SELECT * FROM Customers";
string id = txtSearch.Text.Trim();
if (!string.IsNullOrEmpty(id))
{
id = string.Format(" where Country = '{0}'", id);
}
SqlCommand cmd = new SqlCommand(query + id);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
protected void GridView1_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}