Introduction:
Here I will explain how to populate or display gridview based on dropdownlist selected value in asp.net using c#, vb.net or Bind or filter asp.net gridview based on dropdownlist selection or selected value in asp.net using c#, vb.net or Display or show asp.net gridview based on dropdownlist selection or selected value in asp.net using c#, vb.net.
Description:
In previous articles I explained bind dropdownlist in asp.net gridview using dataset, validate textbox controls inside gridview in asp.net, cascading dropdownlist in gridview using asp.net, change gridview header dynamically in asp.net, Delete multiple rows in gridview using checkbox in asp.net and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to filter asp.net gridview data with dropdownlist selection in in asp.net using c#, vb.net.
In previous articles I explained bind dropdownlist in asp.net gridview using dataset, validate textbox controls inside gridview in asp.net, cascading dropdownlist in gridview using asp.net, change gridview header dynamically in asp.net, Delete multiple rows in gridview using checkbox in asp.net and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to filter asp.net gridview data with dropdownlist selection in in asp.net using c#, vb.net.
Before
implement this example first design one table UserDetails in your database as
shown below
Column Name
|
Data Type
|
Allow Nulls
|
UserId
|
Int(IDENTITY=TRUE)
|
Yes
|
UserName
|
varchar(50)
|
Yes
|
Education
|
varchar(50)
|
Yes
|
Location
|
varchar(50)
|
Yes
|
Once
table created in database enter some dummy data to test application now
open your aspx page and write the code like as shown below
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title> Beautiful Gridview
with Filtering Gridview with Dropdownlist </title>
<%--Styles to Change the appearance of Girdview --%>
<style type="text/css">
.GridviewDiv {font-size: 100%; font-family: 'Lucida
Grande', 'Lucida Sans Unicode', Verdana, Arial, Helevetica, sans-serif;
color: #303933;}
Table.Gridview{border:solid 1px #df5015;}
.GridviewTable{border:none}
.GridviewTable td{margin-top:0;padding: 0; vertical-align:middle }
.GridviewTable tr{color: White; background-color:
#df5015; height:
30px; text-align:center}
.Gridview th{color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;padding:0.5em 0.5em 0.5em 0.5em;text-align:center}
.Gridview td{border-bottom-color:#f0f2da;border-right-color:#f0f2da;padding:0.5em 0.5em 0.5em 0.5em;}
.Gridview tr{color: Black; background-color:
White; text-align:left}
:link,:visited { color: #DF4F13; text-decoration:none }
</style>
</head>
<body>
<form id="form1"
runat="server">
<div class="GridviewDiv">
<table style="width: 420px" border="0"
cellpadding="0"
cellspacing="1"
class="GridviewTable">
<tr >
<td style="width: 40px;">
UserId
</td>
<td style="width: 120px;" >
LastName
</td>
<td style="width: 130px;">
UserName
</td>
<td style="width: 130px;">
Location
</td>
</tr>
<tr >
<td style="width: 40px;">
</td>
<td style="width: 120px;">
</td>
<td style="width: 130px;">
</td>
<td style="width: 130px;">
<asp:DropDownList ID="ddlLocation"
runat="server"
AutoPostBack="true"
Width="120px"
Font-Size="11px"
onselectedindexchanged="ddlLocation_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="4">
<asp:GridView runat="server"
ID="gvDetails"
ShowHeader="false"
AllowPaging="true"
PageSize="10"
AutoGenerateColumns="false"
Width="420px" CssClass="Gridview">
<Columns>
<asp:BoundField DataField="UserId"
HeaderText="UserId"
ItemStyle-Width="40px"
/>
<asp:BoundField DataField="Education"
HeaderText="Education"
ItemStyle-Width="120px"
/>
<asp:BoundField DataField="UserName"
HeaderText="UserName"
ItemStyle-Width="130px"/>
<asp:BoundField DataField="Location"
HeaderText="Location"
ItemStyle-Width="130px"/>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
After completion of aspx page add following namespaces in
codebehind
C#
Code
using System;
using
System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
|
VB.NET
Code
Imports
System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
|
After completion of adding namespaces you need to write the
code like as shown below
C#
Code
string strcon = "Data Source=Suresh;Integrated
Security=true;Initial Catalog=MySampleDB";
protected void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
BindLocation();
BindGridview();
}
}
protected void BindLocation() {
DataTable dt = new DataTable();
using (SqlConnection con = new
SqlConnection(strcon))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select Distinct Location from userdetails",
con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
ddlLocation.DataSource = dt;
ddlLocation.DataTextField = "Location";
ddlLocation.DataValueField = "Location";
ddlLocation.DataBind();
ddlLocation.Items.Insert(0, new ListItem("All",""));
}
}
protected void BindGridview()
{
DataTable dt = new DataTable();
using (SqlConnection con = new
SqlConnection(strcon))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from userdetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs
e)
{
DataTable dt = new DataTable();
using (SqlConnection con = new
SqlConnection(strcon))
{
con.Open();
if (ddlLocation.SelectedValue != "")
{
SqlCommand cmd = new SqlCommand("select * from userdetails where Location
=@location", con);
cmd.Parameters.AddWithValue("@location",
ddlLocation.SelectedValue);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
else
{
SqlCommand cmd = new SqlCommand("select * from userdetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
con.Close();
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
|
VB.NET
Code
Imports
System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Partial Class VBCode
Inherits System.Web.UI.Page
Private strcon As String = "Data Source=Suresh;Integrated
Security=true;Initial Catalog=MySampleDB"
Protected Sub Page_Load(ByVal
sender As Object,
ByVal e As EventArgs) Handles
Me.Load
If Not
IsPostBack Then
BindLocation()
BindGridview()
End If
End Sub
Protected Sub BindLocation()
Dim dt As New DataTable()
Using con As New SqlConnection(strcon)
con.Open()
Dim cmd As New SqlCommand("Select
Distinct Location from userdetails", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
con.Close()
ddlLocation.DataSource = dt
ddlLocation.DataTextField = "Location"
ddlLocation.DataValueField = "Location"
ddlLocation.DataBind()
ddlLocation.Items.Insert(0, New ListItem("All",
""))
End Using
End Sub
Protected Sub BindGridview()
Dim dt As New DataTable()
Using con As New SqlConnection(strcon)
con.Open()
Dim cmd As New SqlCommand("select
* from userdetails", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
con.Close()
gvDetails.DataSource = dt
gvDetails.DataBind()
End Using
End Sub
Protected Sub ddlLocation_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As New DataTable()
Using con As New SqlConnection(strcon)
con.Open()
If ddlLocation.SelectedValue <> "" Then
Dim cmd As New SqlCommand("select
* from userdetails where Location =@location", con)
cmd.Parameters.AddWithValue("@location",
ddlLocation.SelectedValue)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
Else
Dim cmd As New SqlCommand("select
* from userdetails", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
End If
con.Close()
gvDetails.DataSource = dt
gvDetails.DataBind()
End Using
End Sub
End Class
|
Demo
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
1 comments :
i get an error that ddllocation does not have a reference.
i changed the table inputs so my error is ddlgenre does not have a reference
pls help..i need to finish my project(final year) 3 days left
Note: Only a member of this blog may post a comment.