DataGrid with Multiple DropDownLists
page 1 of 1
Published: 05 Nov 2003
Unedited - Community Contributed
Abstract
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.
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: 
Views (Total / Last 10 Days): 9348/ 6

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        



User Comments

Title: Title in Dropdownlist   
Name: Jonathan Nguyen
Date: 2009-09-23 3:45:44 AM
Comment:
great stuff. Thanks for sharing. But i want add item as title (disable to select) how to do it?
http://taphop.net
http://tatca.vn
Title: Need equivalent c# code   
Name: Mohan Raj
Date: 2008-10-29 8:02:38 AM
Comment:
tmpcboState2 = CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex).FindControl("cboState2"), DropDownList);

what is the equivalent of this codes to c#.net.
Title: bind grid   
Name: nitin
Date: 2007-09-05 9:29:02 AM
Comment:
is there any to bind first grid in another grid with data.
Title: dropdownlist   
Name: ainey
Date: 2007-01-15 3:34:24 AM
Comment:
sir...
is there anyway that we can edit the data without the zip dropdownlist being empty? plz help me out...tq in advance.
Title: What about in footer?   
Name: Lesley
Date: 2006-07-13 12:58:22 PM
Comment:
This is great! Thanks! How does one do the same thing but in the datagrid footer? Obviously EditItemIndex won't work...
Title: datagrid control   
Name: john
Date: 2005-12-26 7:23:11 PM
Comment:
sir i would like to ask, if it is possible to add feild in the datagrid control in a runtime in visual basic.net? if it possible, can you give me the code coz i stuck on it
Title: Ing   
Name: Julian Vargas (Colombia)
Date: 2005-11-24 3:18:39 PM
Comment:
Hi rochelle, try this out.

tmpcboState2 = (DropDownList)DG_Combo.Items.Item(DG_Combo.EditItemIndex).FindControl("cboState2")

or

tmpcboState2 = DG_Combo.Items.Item(DG_Combo.EditItemIndex).FindControl("cboState2") as DropDownList
Title: Ing   
Name: Julian Vargas (Colombia)
Date: 2005-11-24 3:15:31 PM
Comment:
thank you very very much
Title: convert to c#.net   
Name: rochelle
Date: 2005-10-10 1:35:38 AM
Comment:
tmpcboState2 = CType(DG_Combo.Items.Item(DG_Combo.EditItemIndex).FindControl("cboState2"), DropDownList);

what is the equivalent of this codes to c#.net. tnx

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


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