-
1
- #1
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:
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.
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
Perhaps this will help a newbie more quickly solve this problem when searching for solutions.