Take the 2-minute tour ×

I have a drop down list that is populated in the page load event from a database table.

The drop down has a DataTextField set to a project name and the DataValueField set to the project id (interger).

Later I change the dropdowlist selected item with this code in the selectedindexchanged event of a gridview

GridViewRow row = GridView1.SelectedRow;
ddlProjectList.SelectedItem.Text = row.Cells[2].Text;

Does Changing the drop down list with this code cause the DataValueField property to change to the correct Project ID number also? If not is there a better way to do this?

=============================================EDIT actually this code seems to be adding an additional item to the list so that the project i set with this code is listed twice so I don't think my code is correct

share|improve this question
Better answer stackoverflow.com/questions/3496456/… –  Four Sep 27 '12 at 13:52

3 Answers 3

up vote 0 down vote accepted

This does not change anything else than the text of the selected item in your DropDownList. Do you want to change the text and value of the project, or do you want to select the project from the DropDownList that relates to the selected row in the grid?

To change the project name and id, you'll have to change it in the data source behind the ProjectList. But if you just want to select the related project, you can use this:

var row = GridView1.SelectedRow;

if (ProjectList.Items.FindByText(row.Cells[2].Text) != null)
    ProjectList.Items.FindByText(row.Cells[2].Text).Selected = true;
share|improve this answer

Setting SelectedItem.Text does not actually change the selected item by its text, it changes the text of the currently selected item. You need to use ddl.Items.FindItemByText (or it may be worded FindByText, I forget at the moment) to find the item, then set the ListItem.Selected property to true.


share|improve this answer

You can do

ddlProjectList.FindByText(row.Cells[2].Text).Selected = true;

This will actually set it.

share|improve this answer

Your Answer


