<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="gvUserInfo"
runat="server"
AutoGenerateColumns="false"
OnRowDataBound="gvUserInfo_RowDataBound"
>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:BoundField DataField="CountryId" HeaderText="CountryId" />
<asp:BoundField DataField="CountryName" HeaderText="CountryName" />
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:DropDownList ID="ddlCity"
runat="server"
Width="100px"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
|
SqlConnection con =new SqlConnection("Data
Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
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
TOP 4 CountryId,CountryName from Country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}
protected void
gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
con.Open();
var ddl = (DropDownList)e.Row.FindControl("ddlCity");
int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
SqlCommand cmd = new SqlCommand("select
* from State where CountryID=" + CountryId, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddl.DataSource = ds;
ddl.DataTextField = "StateName";
ddl.DataValueField = "StateID";
ddl.DataBind();
ddl.Items.Insert(0, new
ListItem("--Select--",
"0"));
}
}
|
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Web.UI.WebControls
Partial Class VBCode
Inherits System.Web.UI.Page
Private con As New SqlConnection("Data
Source=SureshDasari;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
BindGridview()
End If
End Sub
' This method is used to bind
gridview from database
Protected Sub
BindGridview()
con.Open()
Dim cmd As New SqlCommand("select
TOP 4 CountryId,CountryName from Country", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
con.Close()
gvUserInfo.DataSource = ds
gvUserInfo.DataBind()
End Sub
Protected Sub
gvUserInfo_RowDataBound(ByVal sender As Object, ByVal e As
GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
con.Open()
Dim ddl = DirectCast(e.Row.FindControl("ddlCity"), DropDownList)
Dim CountryId As Integer = Convert.ToInt32(e.Row.Cells(0).Text)
Dim cmd As New SqlCommand("select
* from State where CountryID=" & CountryId, con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
con.Close()
ddl.DataSource = ds
ddl.DataTextField = "StateName"
ddl.DataValueField = "StateID"
ddl.DataBind()
ddl.Items.Insert(0, New
ListItem("--Select--", "0"))
End If
End Sub
End Class
|
|
|
|
|
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 Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
12 comments :
Nice one ra
hi suresh garu. u r simply superb.
i want this code without using TOP. is it possible?
Its a valuable site for those who wants to know more about the Asp.net. It sounds good. Keep publishing your articles your blog shall be spreaded to our friends. If real time scenarios are dealt here then it will be more helpful to the freshers.
thanQ suresh... really u helped me a lot
Thanx .. It works ..
thanks suresh very nice and help full
Hi, How can i access the datavalue field from the dropdown list
i can't find id from this line---
int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
i found id by this line----
int id = Convert.ToInt32(GVs.DataKeys[e.Row.RowIndex].Value.ToString());
hey u all...this row databound dat i wanted to use for update the query...
can any1 help to use od update funcion
hi sir,
this is bharath.
i want to populate data in dropdownlist in asp gridview,from sql server data table only.
ex: i am having table country,and i am inserting data to states table with using country name, and i want to populate country data in dropdownlist in aspgridview in states form....,
very nice and easy to understand...
thanks