csharp

Filter Gridview in ASP.net C#

In this article, we will learn how to filter Gridview in ASP.net using C#.We will learn step by step in this post if you will enter text in the textbox and click on search button filter GridView according to name.It fulfills your requirement.Follow my step

[php]

Create database GridViewControl

Use GridViewControl

[/php]

Step 2. Create table

[php]

Create table Employee(
Empid int primary key identity(1,1),
Name varchar(30),
Address varchar(20),
Age int
)

[/php]

Step 3. Insert Data into Employee table

Step 4. Create stored procedure for fetch the data from database

[php]

Create proc sp_emp_get
as
begin
select * from Employee
end

[/php]

Step 5. Open Visual Studio select File & open new Project. Then Select Web and click on Asp. net Empty website. Open web.config file and Establish a connection with database

Note

  • In initial catalog write Database Name
  • In data source you can simply write (.) /your server name/(local)
  • Integrated security for Authentication

[php]
<connectionStrings>
<add name="DBCS" connectionString="data source=.; initial catalog=GridViewControl;integrated security=true"/>
</connectionStrings>
[/php]

Step 6. Open .aspx page drop one textbox, one button and one Gridview Control on design page.

[php]

<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Name: <asp:TextBox ID="txtname" runat="server" ></asp:TextBox>
<asp:Button ID="btnsearch" runat="server" Text="SearchByName" OnClick="btnsearch_Click" />
<br />
<asp:GridView ID="Grid" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
</form>
</body>
</html>
[/php]

Step 7.Open code behind file (.aspx.cs) page.

[php]

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;
using System.Configuration;

namespace GridviewFilter
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
//Now in the page Load call the GridBind Method
if (!IsPostBack)
{
GridBind();
}
}
//Binding The Gridview
public void GridBind()
{
con.Open();
//Stored procedure name–>sp_emp_get for fetch the data
SqlCommand cmd = new SqlCommand("sp_emp_get", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
Grid.DataSource = ds;
Grid.DataBind();
}
//Filterring functionality perform on onClick event
protected void btnsearch_Click(object sender, EventArgs e)
{

con.Open();
SqlCommand cmd = new SqlCommand("select * from Employee where Name=’"+txtname.Text+"’", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
Grid.DataSource = ds;
Grid.DataBind();
}
}
}

[/php]

Screenshot Describe How the filter is work after run the project

DEMO

 

 

Leave a Reply

Your email address will not be published.