AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=143&pId=-1
DataGrid with Multiple DropDownLists
page
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: 
Views (Total / Last 10 Days): 9349/ 7

DataGrid with Multiple DropDownLists

Written on: Aug 16th 2002.
Introduction

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"
          OnSelectedIndexChanged="PopulateNextCombo"
          AutoPostBack="True"
          runat="server" />


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.

PopulateNextCombo
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
     Try
          myConnection.Open()
          myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
          tmpcboState2 = CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex).FindControl("cboState2"), DropDownList)
          tmpcboState2.DataSource = myDataReader
          tmpcboState2.DataBind()
          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()

     Finally
          If Not myDataReader Is Nothing Then
               myDataReader.Close()
          End If

          If Not myConnection Is Nothing AndAlso ((myConnection.State And ConnectionState.Open) = ConnectionState.Open) Then
               myConnection.Close()
          End If
     End Try
End Sub
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

DataGrid with Combo box - 7973 bytes
Fig: DataGrid with Combo Box.

Test this Script

Download the code

Click here to download the ASPX page
Click here to download the Stored Procedure

Conclusion

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 das@aspalliance.com

Links

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 das@aspalliance.com        


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 4:14:13 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search