How to Filter GridView Records with Dropdownlist Selection
In this tutorial i will explain you, How to Filter GridView Records with Dropdownlist Selection.
Design View:-
<%@ 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">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr style="background-color:Green;color:White">
<td>Dept No</td><td>Name</td><td>Location</td>
</tr>
<tr style="background-color:Green;color:White">
<td>Filter By Location</td><td colspan="2">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Loc" DataValueField="Loc" AutoPostBack="true" AppendDataBoundItems="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="All" Value="All"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="Loc" runat="server" ConnectionString="<%$ConnectionStrings:conn %>" SelectCommand="Select Distinct Loc from dept"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowHeader="false">
<Columns>
<asp:BoundField DataField="deptno" HeaderText="dept no" ItemStyle-Width="120px" />
<asp:BoundField DataField="dname" HeaderText="dept name" ItemStyle-Width="120px"/>
<asp:BoundField DataField="Loc" HeaderText="location" ItemStyle-Width="120px"/>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code View:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
binddata();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strcon = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection con = new SqlConnection(strcon);
con.Open();
if (DropDownList1.SelectedItem.Text == "All")
{
string strquery = "select * from dept";
SqlCommand cmd = new SqlCommand(strquery, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
else
{
string strquery = "select * from dept where loc='" + DropDownList1.SelectedItem.Text + "'";
SqlCommand cmd = new SqlCommand(strquery, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
}
public void binddata()
{
string strcon = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection con = new SqlConnection(strcon);
con.Open();
string strquery = "select deptno,dname,loc from dept";
SqlCommand cmd = new SqlCommand(strquery, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
}
Enjoy…..

One reply on “How to Filter GridView Records with Dropdownlist Selection”
I was just searching for this info for some time. After six hours of continuous Googleing, at last I got it in your site. I wonder what is the lack of Google strategy that do not rank this kind of informative sites in top of the list. Generally the top sites are full of garbage.