Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 Setting value to dropdownlist in gridview
Posted by Sam on  4/1/2010 12:41:30 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
I am using a gridview which is showing data using datatable.And i am using ItemTemplate to show data as i have made autogenratecolumn False.The problem is when i do edit then i am binding data to textboxes using eval but when it comes to dropdownlist i am using datatable and fetch data from database and using findcontrol property i do bind data to dropdown.But i am not able to bind the selectedvalue to dropdown which was already selected to that column.

Like someone already select the value of city "Delhi".and when i do it edit dropdown should show the "Delhi" as selected in edit mode.

Post Your Reply Post New Question

 Viewer's Reply
Binding Dropdownlist inside GridView Dynamically
Answered By Moderator1 on 4/1/2010 10:52:44 PM

You need to set the DataKeyNames as the value of the DropDownList control. Then in the GridView GridView1_RowDataBound event, use the below code.

if (e.Row.RowType == DataControlRowType.DataRow)
DropDownList cmbType = (DropDownList)e.Row.FindControl("cmbType");

if (cmbType != null)
cmbType.DataSource = customer.FetchCustomerType();
cmbType.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();
Setting value to dropdownlist in gridview
Answered By sharmeek on 4/2/2010 8:52:35 AM
Post Your Reply If you still have problem, post it again under this reply for better response.

Sponsored by
Backup and Recovery Best Practices for Microsoft SQL Server 2005
No Nonsense XML Web Development With PHP - Free 146 Page Preview!
Washington Technology