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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting dropdown in DataGrid on Edit 2

Status
Not open for further replies.

Isadore

Technical User
Feb 3, 2002
2,167
US
This subject has been discussed many times before, that is, setting the value of a dropbox in a datagrid on edit. The answer is to use the Selected property of the dropdown. Many of the discussions used various other methods, including Session state, Functions, delcaring variables and strings to hold the params, toggling between Visible=True and False, etc. After reviewing about 20 threads I reduced it down to the following which I discovered was the simplest in terms of code (whether its the most efficient is another question). To wit:

Code:
Sub Grid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
 If DataGrid1.EditItemIndex <> -1 And DataGrid1.EditItemIndex = e.Item.ItemIndex Then
  With CType(e.Item.Cells(3).FindControl("ddCategory"), DropDownList)
    .Items.FindByValue(DataBinder.Eval(e.Item.DataItem, "ddCategoryText")).Selected = True
  End With
 End If     
End Sub
Where "ddCategory" is the field name in the Source table used by the label (in the label/dropdown combination - Item and Edit templates) to caputre the current row value; ddCategoryText is the value for that field and the value used to set the dropbox when Edit mode is activated.

Perhaps this will help a newbie more quickly solve this problem when searching for solutions.
 
nice...
could the below be true as well?

//DDL with Integer as DataValueField -
.FindByValue(DataBinder.Eval(e.Item.DataItem, "ddCategoryID")).Selected

//DDL with String as DataValueField -
.FindByText(DataBinder.Eval(e.Item.DataItem, "ddCategoryText")).Selected

as long as ddCategoryText(label text) and ddCategoryID(unshown) is in the dataset

SELECT ddCategoryID, ddCategoryText, FirstName, LastName
FROM tblData
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top