Groups | Blog | Home
all groups > asp.net datagrid control > august 2004 >

asp.net datagrid control : Dependent DropDownLists


stephen
8/31/2004 6:35:34 AM
I am having a problem with my datagrid. I have 2 edit
columns containing DropDownLists, one dependent on
another. Both lists are being populated from a sql
database.

Essentially, the first DropDownList contains a list of
colors (i.e. Red, Blue, Green, etc) with the second
DropDownList containing different shades available for
the selected color (i.e. if Red is selected in the first
DropDownList then the second DropDownList should contain
Scarlet, Blood, Bright, Dark, etc).

I cannot find a way to populate the second DropDownList
each time the user changes their selection in the first
Naveen
9/5/2004 2:07:02 AM
Hi Stephen,

You can implemented this functionality like this :

1. Add an handler for OnSelectedIndexChanged event of your First Drop Down
List.
i.e OnColorChange(ByVal sender As Object, ByVal e As EventArgs)
2. In the Event Handler :
'Get the First DropDown
Dim colorDropDown As DropDownList = CType(sender, DropDownList)
Dim selectedColorID As String = colorDropDown.SelectedItem.Value

'Get the Second DropDown
Dim item As DataGridItem = CType(colorDropDown.Parent.Parent, DataGridItem)
Dim ShadesDropDown As DropDownList = CType(item.FindControl("ddlShades"),
DropDownList)

'Get the Data from DB or Session to populate Second DropDown based on the
Item selected in First Dropdown

dtShades = CType(Session("shades"), DataTable)
dvShades = dtShades.DefaultView
Me.dvShades.RowFilter = String.Empty
If Not selectedColorID = "-1" Then
Me.dvShades.RowFilter = "ColorID ='" & selectedColorID & "'"
End If

With ShadesDropDown
.DataSource = dvShades
.DataValueField = "ShadeId"
.DataTextField = "ShadeName"
.DataBind()
End With

Hope It helps :)


[quoted text, click to view]
AddThis Social Bookmark Button