Filter gridview with textbox in Asp.net
In this article I'm going to explain how to filter GridView with TextBox in asp.net. For example GridView load huge data. Here is my requirement is to filter particular records based on name by entering into TextBox, let me show you how to achieve this.
In my previous articles i explained about : How to Bind Data in Dropdownlist in Gridview Description :
In this article i'm going to explain how to filter gridview with textbox in asp.net. For example gridview load huge data. Here is my requirement is to filter particular records based on name by entering into textbox, let me show you how to achieve this.Create Database :
copy and paste this code in your sql server query window and click on execute button.
CREATE TABLE [dbo].[Names](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NULL,
[city] [nvarchar](50) NULL,
[country] [nvarchar](50) NULL
) ON [PRIMARY]
GO HTML MarkUp Page :
In Design view drag and drop Textbox Control, Label Control and Gridview Control.
set properties of above controls like:Textbox Control :
ID = txt_get_data_in_grid ( rename Textbox1 to txt_get_data_in_grid)
AutoPostBack = true ( change false to true)
TextChanged = Double Click on TextChanged EventLable Control :
ID = lbl_error (rename Label1 to lbl_error)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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">
<style type="text/css">
.grdheader
{
font-weight: bold;
background-color:DarkBlue;
color:White;
position: relative;
}
.grdscrollpager
{
border-top: 1px solid #AAAAAA;
background-color: #FFFFFF;
}
</style>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Name : <asp:TextBox ID="txt_get_data_in_grid" runat="server"
Width="180px" AutoPostBack="True"
ontextchanged="txt_get_data_in_grid_TextChanged"></asp:TextBox><br /><br />
<asp:GridView ID="GridView1" runat="server">
<HeaderStyle Font-Bold="True" ForeColor="White" CssClass="grdheader" />
<PagerStyle ForeColor="White" HorizontalAlign="Center" CssClass="grdscrollpager" />
</asp:GridView>
</div>
<asp:Label ID="lbl_error" runat="server"></asp:Label>
</form>
</body>
</html> Namespaces :
Import namespace to access some default functionality .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient; C# Coding for Load Data into Gridview :
this below code is used to Bind Data from Database table to Gridview
public void LoadGridview()
{
SqlDataAdapter da = new SqlDataAdapter("Select id as ID, name as CustomerName, city as City, country as Country from Names", con);
DataSet ds = new DataSet();
int n = da.Fill(ds);
if (n > 0)
{
GridView1.Visible = true;
GridView1.DataSource = ds;
GridView1.DataBind();
lbl_error.Text = "";
}
}C# Coding for Textbox Change Event :
protected void txt_get_data_in_grid_TextChanged(object sender, EventArgs e)
{
string query = "select id as ID, name as CustomerName, city as City, country as Country from Names where name like '" + txt_get_data_in_grid.Text + "%'";
SqlDataAdapter da1 = new SqlDataAdapter(query, con);
DataSet ds1 = new DataSet();
int n1 = da1.Fill(ds1);
if (n1 > 0)
{
GridView1.Visible = true;
GridView1.DataSource = ds1;
GridView1.DataBind();
lbl_error.Text = "";
}
else
{
if (n1 == 0)
{
GridView1.Visible = false;
lbl_error.Text = "No Record Found...";
}
else
{
LoadGridview();
}
}
}
Nice code, required lot of formatting.