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

Bind data to dropdownlist in gridview

May 27, 2012

In this article I will explain how to bind data to dropdownlist in inside of gridview in


In previous post I explained article how to bind data to textbox control in gridview in Now I will explain how to bind data to dropdownlist in inside of gridview in

To implement this concept first design tables in your database as explained in this post populate dropdown based on another dropdown in . After that design your aspx page like this 

<html xmlns="">
<head id="Head1" runat="server">
<title> Bind Data to Dropdownlist in inside of gridview</title>
<form id="form1" runat="server">
<asp:GridView ID="gvUserInfo" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvUserInfo_RowDataBound" >
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<asp:BoundField DataField="CountryId" HeaderText="CountryId" />
<asp:BoundField DataField="CountryName" HeaderText="CountryName" />
<asp:TemplateField HeaderText="Location">
<asp:DropDownList ID="ddlCity" runat="server" Width="100px"/>
Now add following namespaces in codebehind

C# Code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
After that add following code in code behind

SqlConnection con =new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
// This method is used to bind gridview from database
protected void BindGridview()
SqlCommand cmd = new SqlCommand("select TOP 4 CountryId,CountryName from Country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
gvUserInfo.DataSource = ds;


protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
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();
ddl.DataSource = ds;
ddl.DataTextField = "StateName";
ddl.DataValueField = "StateID";
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
End If
End Sub
' This method is used to bind gridview from database
Protected Sub BindGridview()
Dim cmd As New SqlCommand("select TOP 4 CountryId,CountryName from Country", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
gvUserInfo.DataSource = ds

End Sub

Protected Sub gvUserInfo_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
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()
ddl.DataSource = ds
ddl.DataTextField = "StateName"
ddl.DataValueField = "StateID"
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,, 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


Anonymous said...

Nice one ra

Anonymous said...

hi suresh garu. u r simply superb.

Gaurang Raval "Yogi" said...

i want this code without using TOP. is it possible?

Anonymous said...

Its a valuable site for those who wants to know more about the 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.

anjali said...

thanQ suresh... really u helped me a lot

Anonymous said...

Thanx .. It works ..

thenrajan said...

thanks suresh very nice and help full

sandeep kulkarni said...

Hi, How can i access the datavalue field from the dropdown list

Anonymous said...

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());

Vinu Mali said...

hey u all...this row databound dat i wanted to use for update the query...
can any1 help to use od update funcion

Anonymous said...

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....,

Santosh said...

very nice and easy to understand...

Anonymous said...

I did in the above way,
but i am facing the problem is that when grid page index is changed , the selected value of drop down is missing

Anonymous said...

use AutoPostBack="true" where dropdownlist declared

Anonymous said...

I am getting error like:
Multiple controls with the same ID 'One' were found. FindControl requires that controls have unique IDs.
What I have to do?

Anonymous said...


Anonymous said...

my case is click edit button then filling dropdownlist.its fine.when i click update i want to check validation of to do?

Anonymous said...

it,s useful rreally

Anonymous said...

Its really usefull site...

jana roja said...

hai suresh
this is janardhan
this is usefull a lot for me

Anonymous said...

≥ ≤

Anonymous said...

Thanks sir...Ur Article helped me a lot

The Happy Human Jellyfish said...

I would be nowhere without your website for SURE. Thankyou sir. You are a really good teacher!

Anonymous said...


Ajay Shedge said...
This comment has been removed by the author.
Ajay Shedge said...

Hello sir,
can we fill dropdown from database in gridview by other way?
Please Tell me

Sunil Kumar said...

Hi sir,
I want a grid with two dropdowns , 1 for states , 2 for districts and selectedindexchanged of 1 will fill items in 2 dropdown.
Also there should be add new row button which will insert blank new row.
I am working on the same everything is ok but when i insert new row all previous dropdowns changed to selected index 1, pl help.

Anonymous said...

simply superb bro

Bino Mathew Varghese said...

How Can i Update the values to database from the dropdown list.?

Anonymous said...

hello i want one more dropdown on next row and on selected index changed of state city will to do that?

Give your Valuable Comments

Other Related Posts

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