ASP.Net GridView DropDownList DataSource and DataBinding

Updated on 31 Jul 2012,
Published on 07 Aug 2008

In the previous article ASP.Net GridView Template DropDownList we discussed about the HTML code for customizing the GridView Template Field columns to display the order_id and nested DropDownList control inside the second TemplateField column. Here we will complete the next step of DataBinding using the DataSource property of GridView control. We will use C# foreach loop over the rows of GridView control after databinding. C# foreach loop will allow using the FindControl method to get the reference of DropDownList control placed inside the ItemTemplate of GridView Control. FindControl method helps to pass the reference of encapsulated server control such as DropDownList control in this tutorial to the object of the related control class.

C# Code for ASP.Net GridView Control DataSource and DataBinding

// SQL Command object to pass the SQL query for retrieving the data from tbl_orders table.
SqlCommand mySqlCommand = new SqlCommand("select * from tbl_orders", mySQLconnection);

SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);

DataSet myDataSet = new DataSet();


// GridView Data Control Bound to the DataSet filled with data retrieved from tbl_orders.
GridView1.DataSource = myDataSet;


You can use the above code to bind the data stored in the SQL Database tbl_orders table. Free source code of this example have complete C# code for database connectivity as well as functionality to display the data in GridView and selectedValue of DropDownList control placed in it.

C# Code for ASP.Net GridView DropDownList DataSource

// Here new SQL query is pass to the SQL command object to get the data from tbl_order_status table.
mySqlCommand = new SqlCommand("select * from tbl_order_status", mySQLconnection);

mySqlAdapter = new SqlDataAdapter(mySqlCommand);

myDataSet = new DataSet();


// DropDownList Control Object Created to bind the data dynamically with each nested DropDownlist control placed inside the template column of the GridView Control.
DropDownList drdList;

// foreach loop is used to loop through each row of GridView Control.
foreach (GridViewRow grdRow in GridView1.Rows)
    // Nested DropDownList Control reference is passed to the DrdList object. This will allow you access the properties of dropdownlist placed inside the GridView Template column.
    drdList = (DropDownList)( GridView1.Rows[ grdRow.RowIndex ].Cells[1].FindControl( "DropDownList1" ));

    // DataBinding of nested DropDownList Control for each row of GridView Control.
    drdList.DataSource = myDataSet;
    drdList.DataValueField = "order_status_id"; 
    drdList.DataTextField = "order_status_name";

In the above sample code, note that objects of SqlCommand, SqlDataAdapter and DataSet has been reused for retrieving the data stored in tbl_order_status table. DataSet object is filled outside the C# foreach loop over the GridView Rows coz data is to be retrieved once and reused each time to bind it with nested DropDownList control placed inside the GridViewRows. Just below the Fill method code of SQLDataAdapter you can see the object created for DropDownList to access the properties of this control. Inside C# code block of foreach loop with first code line having FindControl method to find the DropDownList control. 1 is passed as value to the Cells collection object of TableCellCollection coz DropDownList was placed inside the second Template column of column GridView i.e. at 1st index. C# foreach loop has been used to find the DropDownList in each GridViewRow generated after DataBinding.




Get the output of above discussed code from the following link:

GridView DropdownList DataBinding

In the next tutorial Gridview Dropdownlist SelectedValue in ASP.Net you will get the C# code to set the SelectedValue of Dropdownlist placed inside the GridView control.

15 Responses to "ASP.Net GridView DropDownList DataSource and DataBinding"
You are genius !
thank are damn genius
Nice article from you....
1.I have some problem with dropdown in gridview.
i want to show dropdown in gridview when page load and that grid contains only one row in which having different columns.In those columns i have one dropdown and i want to display 10 values in dropdown from database with single row in gridview.But i am getting 10 rows in gridview not single to display all those dropdown values in single dropdown.
2.I have one button(add) in that same page(button was not in gridview),when user clicks that button a new row(i.e if gridview contains 2 row than when he clicks add button 3 rows should append)will be added.
Its very urgent for me please reply to this....
u r given nice coding
To place a dropdown in gridview,it should be placed inside else
r u great
Sohaib Qazi
Hi I want to add two dropdownlists in a single datagrid cell, Dropdownlist will pull the data by using sqldatasource. how can i do it by using Web Forms of Any little help will be appreciated greatly in this regard....
Hi Sohaib

Please give some idea about the dataset code you are using to fill both the dropdownlist controls.
Nitin H. Chauhan
Hello Genius,

I want to Show 10 rows of grid view on page load.There are 1 drop down list and 1 text box in two columns. Drop Down List Fill Dynamically where page load.I want both value for save this data to table.

Hi Nitin

You can learn it from the next tutorial here:
Edit Update GridView DropDownList SelectedValue

Hope it will help you.

Good Luck ;)
Thanks Buddy this code works...
Hey Really gr8.. Thanks a lot........
thanks a lot....
ur code helped me in my work...
finding for this only...
i think it is hard code and anyway so thanks
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email