Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting drop down list value

Status
Not open for further replies.

jockey

Programmer
Nov 18, 2003
34
GB
I have a datagrid that I have set up with edit templates. When edit is pressed the selected row displays the required drop down list and thedse lists are populated with the required value. The problem I am having is that I am unable to set the selectedvalue for the drop down lists so they display the values previously entered on the grid. I have looked at several posts here and have not found an answer. Does anyone know what I neeed to do??

Many Thanks

Jamie
 
Use the OnItemDataBound event. When you reach your SelectedItem, grab your dropdown, along with the associated data, and set the SelectedItem.

Here, I'm assuming you are using a DataTable called myDataTable as the datasource for your grid.
Code:
protected void OnItemDataBound(object sender, DataGridItemEventArgs e)
{
switch(e.Item.ItemType)
{
  case ListItemType.SelectedItem:
    DropDownList myDropDown = (DropDownList)e.Item.FindControl("myDropDown");
    string myItem = myDataTable.Rows[e.Item.ItemIndex]["myColumnName"].ToString();
    try
    {
      myDropDown.Items.FindByValue(myItem).Selected = true;
    }//try
    catch{}
    break;
}//switch
}

hope this helps.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top