Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Display Gridview Based on Dropdownlist Selection Value in Asp.net using C#, VB.NET

Jun 11, 2015
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.


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

Display Gridview Based on Dropdownlist Selection Value in Asp.net using C#, VB.NET


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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.