Using a drop down list to edit datagrid items
Hi,
I'm trying to setup a datagrid which is databound, but when editing a particular column, rather than a text box, the options are displayed in a dropdown list. I'm setting up the datagrid as shown below.
Code:
<asp:DataGrid id="grdSPs" runat="server">
<Columns>
<asp:BoundColumn DataField="SP_Name"</asp:BoundColumn>
<asp:BoundColumn DataField="SP_Cnt"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<%# Container.DataItem("TT_Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="cmbEditT" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" EditText="Edit">
</asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
The problem I have is getting a link to the dropdown list in the edit template column.
The code behind I've tried is...
Code:
Private Sub grdSPs_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdSPs.EditCommand
grdSPs.EditItemIndex = e.Item.ItemIndex
SetupEditTable() 'This calls a routine to bind the datagrid and is working fine.
'Call routine to retrieve dataset using stored procedure
'This is working fine, the Table is created fine in the dataset
Dim TDataSet As DataSet = GetDataSet("GetTerritories", EF, "TTable")
'Now I'm trying to get a link to the dropdown list
Dim cmbTList As System.Web.UI.WebControls.DropDownList
cmbTList = e.Item.FindControl("cmbEditT")
'This always returns NOTHING
'Once I can get the above reference to work, I should be OK
End Sub
I'm thinking that maybe the dropdown list control isn't actually created during the EditCommand routine. Any help would be much appreciated.
Thanks.
**RESOLVED** Using a drop down list to edit datagrid items
Found some useful code on the net on using the OnItemDataBound event....
Code:
Private Sub grdSPs_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdSPs.ItemDataBound
Dim itemType As ListItemType = e.Item.ItemType
If (itemType = ListItemType.EditItem) Then
Dim cmbList As DropDownList = CType(e.Item.FindControl("cmbEditT"), DropDownList)
Dim drItem As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim myTT As String = myRow("TT_Index").ToString()
myList.DataSource = GetTTDataTable
myList.DataValueField = "TT_Index"
myList.DataTextField = "TT_Name"
myList.DataBind()
myList.Items.FindByValue(myTT).Selected = True
End If
End Sub