Filter Gridview with Textbox in asp.net by using C#


In my Previous Article i explained about :
  1. Retrieve Data from Database and Bind Gridview through DB Class
  2. Select or Deselect Multiple Checkbox in Gridview
  3. Export Gridview Data to Excel Sheet
filter Gridview by using textbox data

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.

HTML MarkUp : Design


<%@ 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">
    <link href="CSS/StyleSheet.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
    <script type="text/javascript" src="http://gridviewscroll.aspcity.idv.tw/"></script>
    <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>

C# Coding

C# Coding : Namespace


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 : Page Load


public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Server=.; Database = master; Trusted_Connection = Yes;");

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadGridview();
    }
}

C# Coding : Bind Gridview


    public void LoadGridview()
    {        
        SqlDataAdapter da = new SqlDataAdapter("Select * 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 : TextBox TextChanged Event


    protected void txt_get_data_in_grid_TextChanged(object sender, EventArgs e)
    {
        string query = "select * 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();        
            }
        }
    }
}
2 Komentar untuk "Filter Gridview with Textbox in asp.net by using C#"

Export Gridview To Excel Sheet in Asp.net by Using C# Coding

http://allittechnologies.blogspot.com/2015/05/gridview-data-export-to-excel.html

How to do Edit, Update, Delete in Asp.net Gridview by Using C# Coding

http://allittechnologies.blogspot.in/2015/05/gridview-edit-update-delete-button-in-asp.net-using-csharp-in-software-it-technologies.html

Back To Top