DataGrid with Multiple DropDownLists
Written on: Aug 16th 2002.
This article is continuation of DataGrid and DropDownList.
In this article, we will discuss populating a DropDownList based on the SelectedItem from a different DropDownList, that is, how to populate DropDownLists that have a master detail relationship.
BackBone of this article
|<asp:DropDownList id="cboState" |
How it works?
The above declaration pertains to the master DropDownList. Please note that the AutoPostBack property is set to true. This is the key aspect. When a user changes the current value in the master DropDownList, we should populate the child DropDownList. That is why we have set the OnSelectedIndexChanged property.
|Public Sub PopulateNextCombo(ByVal sender As Object, ByVal e As System.EventArgs)|
Dim tmpcboState2 As DropDownList
Dim tmpDropDown1 as DropDownList
Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim myCommand As SqlCommand = New SqlCommand("sp_das_state_sel", myConnection)
Dim myDataReader As SqlDataReader
myCommand.CommandType = CommandType.StoredProcedure
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
tmpcboState2 = CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex).FindControl("cboState2"), DropDownList)
tmpcboState2.DataSource = myDataReader
tmpDropDown1 = CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex).FindControl("cboState"), DropDownList)
tmpcboState2.SelectedIndex = tmpDropDown1.SelectedIndex
Catch SQLexc As SqlException
lblStatus.Text = "Error while Generating Data. Error is " & SQLexc.ToString()
If Not myDataReader Is Nothing Then
If Not myConnection Is Nothing AndAlso ((myConnection.State And ConnectionState.Open) = ConnectionState.Open) Then
How It Works
The above method populates the second (child) DropDownList. cboState2 is the ID of our second DropDownList. The third statement inside the Try block gets the reference for the second DropDownList. The fourth and fifth statements in the Try block populate the second DropDownList. In this example, we are setting the SelectedItemIndex for the second DropDownList to be the same as the master DropDownList.
Sample output of our scenario
Test this Script
Fig: DataGrid with Combo Box.
Download the code
Click here to download the ASPX page
Click here to download the Stored Procedure
I have used stored procedures in all places. For your convenience, I have also included source code for these stored procedures. If you have any questions regarding this article or any of my article please feel free to email me at firstname.lastname@example.org
DataGrid with Single Combo box
How to Edit a DataGrid?
How to capture a Double Click event in DataGrid?
Checkbox Web Server Control
Retrieving Images from SqlServer in ASP .NET
Retrieving Images from SqlServer and displaying it in DataGrid
Send your comments to email@example.com