Parent-Child dropdown in a grid
-
Hi Folks, I had a situation, where i need to have two drop downs in the edit mode of a datagrid. In the edit mode if i select a value from one of the drop down (ex say a country) the other drop down which is part of another column should be re-populated based on the previous drop downs selection. This is a typical parent/child or category/sub category relationship. The problem i am facing at the moment is to get a reference to the child drop down in the edit mode. Cheers, Venkatraman Kalyanam Bangalore - India Reality bites: I am reality
-
Hi Folks, I had a situation, where i need to have two drop downs in the edit mode of a datagrid. In the edit mode if i select a value from one of the drop down (ex say a country) the other drop down which is part of another column should be re-populated based on the previous drop downs selection. This is a typical parent/child or category/sub category relationship. The problem i am facing at the moment is to get a reference to the child drop down in the edit mode. Cheers, Venkatraman Kalyanam Bangalore - India Reality bites: I am reality
Set parent dropdownlist property AutoPostBack = True. Set up an event handler for the parent dropdownlist in the ItemCreated event of the DataGrid. Write the event handler mentioned above. Private Sub DataGrid1_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated If e.Item.ItemType = listitemtype.EditItem Then Dim d As DropDownList = e.Item.Cells(3).FindControl("ddlProtocol") 'must add event handler for this dropdownlist AddHandler d.SelectedIndexChanged, AddressOf DoDdlSelectedIndexChanged 'Get items to add to dropdownlist Dim List() as String = GetItemsForList() 'fill list d.Items.Add("Not Selected") For i As Integer = 0 To UBound(List) d.Items.Add(List(i)) Next End If End Sub Private Sub DoDdlSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Dim ParentListValue As String ParentListValue= DirectCast(sender, DropDownList).SelectedValue Dim List() As String = GetChildListItems(ParentListValue) 'get a reference to the child dropdown (in 5th column of grid) Dim d As DropDownList = DirectCast(DirectCast(DirectCast(DirectCast(sender, DropDownList).Parent, TableCell).Parent, DataGridItem).Cells(4).FindControl("dropdownlist2"), DropDownList) d.Items.Clear() d.Items.Add("Not Selected") For j As Integer = 0 To UBound(List) d.Items.Add(List(j)) Next End Sub