Gridview DropDownList SelectedValue in ASP.Net

Updated on 03 Aug 2012,
Published on 07 Aug 2008

SelectedValue property of each DropDownList placed inside the rows of GridView control can be set using the value of order_status_id field of tbl_orders and tbl_order_status. As we discussed in the previous article ASP.Net Gridview DropdownList Column order_status_id is a relational foreign key of tbl_orders table and primary key of tbl_order_status table. In the next tutorial ASP.Net Gridview Template DropDownList we created the HTML code for GridView control with nested DropDownList and DataKeyNames collection property of GridView was assigned column names i.e. order_id and order_status_id. In this tutorial we will use these DataKeys collection to set the SelectedValue property of each DropDownList with its corresponding value of order_status_id stored in the DataKeyNames collection.

Consider the C# Code for ASP.Net GridView DropDownList DataSource and databinding of previous tutorial in which we used the C# foreach loop to bind the list items retrieved from tbl_order_status table with DropDownList control inside the GridView Control:

// 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";

    // GridView DataKeys Collection object is used here to set the SelectedValue property of nested DropDownList in each row.
    drdList.SelectedValue = GridView1.DataKeys[grdRow.RowIndex]["order_status_id"].ToString();

Last C# code lines show the code to get the value of order_status_id from the DataKeys collection of GridView control and set it as the SelectedValue property of DropDownList of that particular Row of GridView.


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

GridView DropdownList Set SelectedValue

In the next tutorial Edit Update GridView DropDownList SelectedValue you will learn how to save the SelectedValue of Dropdownlist control into the database table using C# code.

13 Responses to "Gridview DropDownList SelectedValue in ASP.Net"
good work
good work
It nice to work
i am from sri lanka.
thank you.
Not sure but I got displayed in my dropdown. ddlPlatform.DataSource = tempTable2; ddlPlatform.DataBind(); did I miss something?
while working with dropdown list control, i am getting an error like:

HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

what is the solution?
plz! help me in this regard.
Plz send answer to my mail id
Hi Krishna

Please check the URL specified that loads after postback event associated with selected index changed event of dropdownlist.
It is really very helpful
Thank you
This is by far the best code I've seen on this topic on the internet. I would like to personally thank you and I look forward to reading your blog more. Thanks for recommending the bugchecker, it's nice.
This is good but any simplfication for number of dropdown list in gridview in entity type. It also work,But I add a some modification for that But It is much better than that previous stage Thank you
Thank you!!!!!!!!!!!
Really nice. Check this article. Its explained well with a demo. Rimi
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email