Introduction:
In this article I will explain how to bind data to dropdownlist in gridview in asp.net using dataset. This is the common requirement while working with gridview to populate dropdown list inside the gridview.
Implementation: Let's create a demo
website to demonstrate the concept.
Create Database Test and two
table “State & City”.
Column Name
|
Data Type
|
Allow Nulls
|
Stateid
|
Int(IDENTITY=TRUE)
|
Yes
|
StateName
|
varchar(50)
|
Yes
|
Column Name
|
Data Type
|
Allow Nulls
|
Cityid
|
Int(IDENTITY=TRUE)
|
Yes
|
CityName
|
varchar(50)
|
Yes
|
Stateid
|
int
|
Yes
|
Source Code: Sample Application
Design Page: design your page as given below
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Asp.net Bind Data to Dropdownlist in
inside of gridview</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:GridView ID="gvInfo"
runat="server"
AutoGenerateColumns="false"
OnRowDataBound="gvInfo_RowDataBound"
>
<HeaderStyle BackColor="#982424"
Font-Bold="true"
ForeColor="White"
/>
<Columns>
<asp:BoundField DataField="StateId"
HeaderText="StateId"
/>
<asp:BoundField DataField="StateName"
HeaderText="State"
/>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:DropDownList ID="ddlCity"
runat="server"
Width="100px"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Asp.Net Code Behind File(using C#)
Write following code in code behind file:
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;
public partial
class state
: System.Web.UI.Page
{
SqlConnection
con = new SqlConnection("Data Source=localhost;Integrated
Security=true;Initial Catalog=test");
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridview();
}
}
// This method is
used to bind gridview from database
protected void BindGridview()
{
con.Open();
SqlCommand
cmd = new SqlCommand("select * from state", con);
SqlDataAdapter
da = new SqlDataAdapter(cmd);
DataSet
ds = new DataSet();
da.Fill(ds);
con.Close();
gvInfo.DataSource = ds;
gvInfo.DataBind();
}
protected void gvInfo_RowDataBound(object
sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
con.Open();
var
ddl = (DropDownList)e.Row.FindControl("ddlCity");
int
StateId = Convert.ToInt32(e.Row.Cells[0].Text);
SqlCommand
cmd = new SqlCommand("select * from city where stateID=" +
StateId, con);
SqlDataAdapter
da = new SqlDataAdapter(cmd);
DataSet
ds = new DataSet();
da.Fill(ds);
con.Close();
ddl.DataSource = ds;
ddl.DataTextField = "CityName";
ddl.DataValueField = "CityID";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
}
Asp.Net Code Behind File(using VB.NET)
Write following code in code behind file:
Imports System.Data.SqlClient
Imports System.Data
Partial Public
Class state
Inherits
System.Web.UI.Page
Dim con As SqlConnection =
New SqlConnection("Data Source=localhost;Integrated
Security=true;Initial Catalog=test")
Protected Sub Page_Load(ByVal
sender As Object,
ByVal e As EventArgs)
If Not IsPostBack Then
BindGridview()
End If
End Sub
' This method is
used to bind gridview from database
Protected Sub BindGridview()
con.Open()
Dim cmd
As SqlCommand
= New SqlCommand("select * from state", con)
Dim da As SqlDataAdapter = New
SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
da.Fill(ds)
con.Close()
gvInfo.DataSource = ds
gvInfo.DataBind()
End Sub
Protected Sub gvInfo_RowDataBound(ByVal
sender As Object,
ByVal e As GridViewRowEventArgs)
If
e.Row.RowType = DataControlRowType.DataRow Then
con.Open()
Dim
ddl As var = CType(e.Row.FindControl("ddlCity"), DropDownList)
Dim
StateId As Integer
= Convert.ToInt32(e.Row.Cells(0).Text)
Dim
cmd As SqlCommand
= New SqlCommand("select * from city where stateID=" +
StateId, con)
Dim
da As SqlDataAdapter
= New SqlDataAdapter(cmd)
Dim
ds As DataSet
= New DataSet()
da.Fill(ds)
con.Close()
ddl.DataSource = ds
ddl.DataTextField = "CityName"
ddl.DataValueField = "CityID"
ddl.DataBind()
ddl.Items.Insert(0, New ListItem("--Select--", "0"))
End If
End Sub
End Class
0 comments:
Post a Comment