473,503 Members | 3,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dropdown lists in gridviews on selected index changed.

6 New Member
Hi

Is it possible to change the data source of a dropdown list in a gridview from another dropdown list selected index changed method in the same gridview?

for example I have a dropdown that needs to change its contents depending on what is chosen in the previous cell of the gridview, which is also a dropdown list.

Any Help would be much appreciated

Thanks
Nov 16 '09 #1
5 20992
Frinavale
9,735 Recognized Expert Moderator Expert
Have you considered accessing the row that the DropDownList belongs to using the NamingContainer?

For example:
VB:
Expand|Select|Wrap|Line Numbers
  1. Protected Sub FirstDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles FirstDropDownList.SelectedIndexChanged
  2.  
  3.   'Grabbing a reference to the row that the DropDownList belongs to:
  4.   Dim gridViewRowDdlBelongsTo As GridViewRow  = Ctype( Ctype(sender,Control).NamingContainer, GridViewRow)
  5.  
  6.   'Grabbing a reference to the second DropDownList in the row:
  7.    Dim secondDropDownList As DropDownList = CType(gridViewRowDdlBelongsTo.FindControl("secondDropDownList"),DropDownList)
  8.  
  9.   'Now you just need to set the data source for the secondDropDownList
  10.   'according to the selection in the firstDropDownList
  11.   '.......
  12. End Sub
  13.  
C#:
Expand|Select|Wrap|Line Numbers
  1. protected void FirstDropDownList_SelectedIndexChanged(object sender, EventArgs e)    {       
  2.   // Grabbing a reference to the row that the DropDownList belongs to:
  3.   GridViewRow gridViewRowDdlBelongsTo = (GridViewRow)(((Control)sender).NamingContainer);   
  4.  
  5.   // Grabbing a reference to the second DropDownList in the row:
  6.   DropDownList secondDropDownList = (DropDownList)gridViewRowDdlBelongsTo .FindControl("secondDropDownList");
  7.  
  8.   // Now you just need to set the data source for the secondDropDownList
  9.   // according to the selection in the firstDropDownList
  10.   // ....
  11. }
-Frinny
Nov 16 '09 #2
Kalkin
6 New Member
Thank you for replying so promptly. I have tried what you suggested, and i must admit that i am quite new to programming so i might have misunderstood. But here is my code never the less.

Expand|Select|Wrap|Line Numbers
  1. Protected Sub ddMerchCat_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
  2.         Dim objRecordsource As New Bestvest_DataAccess.cDataAccess
  3.         Dim dstSubMerch As New DataSet
  4.         Dim strMerch As String
  5.         Dim ddMerchCat As DropDownList = CType(gvRentalIncome.Rows(0).FindControl("ddMerchCat"), DropDownList)
  6.  
  7.         Dim drwSubMerch As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
  8.  
  9.         strMerch = Replace(ddMerchCat.SelectedItem.Text, " ", "")
  10.         dstSubMerch = objRecordsource.getRecordSet("Select * from tbl_Merc_" & strMerch & "")
  11.  
  12.         Dim ddSubMerch As DropDownList = CType(drwSubMerch.FindControl("ddSubMerchCat"), DropDownList)
  13.         ddSubMerch.DataSource = dstSubMerch
  14.         ddSubMerch.DataValueField = "" & strMerch & "ID"""
  15.         ddSubMerch.DataTextField = "Type"
  16.         ddSubMerch.DataBind()
Nov 17 '09 #3
Frinavale
9,735 Recognized Expert Moderator Expert
Is there a problem with your code?
I don't see anything wrong...

Here's a working example of what you're trying to do. I have 2 columns in my GridView. The first column contains a DropDownList named "ddl1" and the second column contains a DropDownList named "ddl2". By default ddl1 displays numbers 1-10 and ddl2 doesn't have any items. If you select an even number in ddl1 then ddl2 is populated with letters but if you select an odd number in ddl1 then ddl2 is populated with a list of roman numerals.

In my Default.aspx page I have the following (very simple GridView):
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="MyNamespace._Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title>Testing DropDownLists</title>
  7. </head>
  8. <body>
  9.     <asp:GridView ID="theGridView" runat="server" AutoGenerateColumns="false">
  10.         <Columns>
  11.         <asp:TemplateField HeaderText="First DropDownList" ItemStyle-HorizontalAlign="Center">
  12.             <ItemTemplate> <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl1_OnSelectedIndexChanged"></asp:DropDownList></ItemTemplate>
  13.         </asp:TemplateField>
  14.         <asp:TemplateField HeaderText="Second DropDownList" ItemStyle-HorizontalAlign="Center">
  15.             <ItemTemplate><asp:DropDownList ID="ddl2" runat="server"></asp:DropDownList></ItemTemplate>
  16.         </asp:TemplateField>
  17.         </Columns>
  18.     </asp:GridView>
  19.     </form>
  20. </body>
  21. </html>
And here is my very simple VB code:
Expand|Select|Wrap|Line Numbers
  1. Partial Public Class _Default
  2.   Inherits System.Web.UI.Page
  3.  
  4.   Private src As DataView 'used as the datasource for the GridView
  5.   Private ddl1Source As List(Of ListItem) 'used as the datasource for the first DropDownList in the GridView
  6.  
  7.  
  8.   ''' <summary>
  9.   '''  Creates a simple data source to be used in the GridView
  10.   ''' </summary>
  11.   ''' <remarks></remarks>
  12.   Private Sub GenerateSource()
  13.     'Only creating a new source if one doesn't already exist in session.
  14.     If Session("src") Is Nothing Then 
  15.       Dim tbl As New DataTable
  16.       tbl.Columns.Add("ID")
  17.       tbl.Columns.Add("first")
  18.       tbl.Columns.Add("second")
  19.  
  20.       For i As Integer = 1 To 10
  21.         Dim r As DataRow = tbl.NewRow()
  22.         r("ID") = i.ToString
  23.         tbl.Rows.Add(r)
  24.       Next
  25.       src = New DataView(tbl)
  26.       Session("src") = src
  27.  
  28.       'Generating the default source for the first DropDownList:
  29.       ddl1Source = New List(Of ListItem)
  30.       For i As Integer = 1 To 10
  31.         ddl1Source.Add(New ListItem(i.ToString, i.ToString))
  32.       Next
  33.       Session("ddl1Source") = ddl1Source 
  34.     Else
  35.       src = CType(Session("src"), DataView)
  36.       ddl1Source = CType(Session("ddl1Source"), List(Of ListItem))
  37.     End If
  38.   End Sub
  39.  
  40.   ''' <summary>
  41.   '''  Handles the Page PreRender event. This happens just before the
  42.   ''' the page's controls are rendered as HTML and sent to the browser.
  43.   ''' </summary>
  44.   ''' <param name="sender">The Object that raised the event (the Page).</param>
  45.   ''' <param name="e">The EventArgs for the event.</param>
  46.   ''' <remarks></remarks>
  47.   Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  48.     If IsPostBack = False Then
  49.       'If it's the first time loading the page, generating the data source
  50.       'for the GridView and binding to it.
  51.       GenerateSource()
  52.       theGridView.DataSource = src
  53.       theGridView.DataBind()
  54.     End If
  55.   End Sub
  56.  
  57.   ''' <summary>
  58.   ''' Handles theGridView's RowDataBound Event. Here I am setting the
  59.   ''' data source for the first DropDownList and binding it to it.
  60.   ''' </summary>
  61.   ''' <param name="sender">The Object that raised the event (theGridView).</param>
  62.   ''' <param name="e">The GridViewRowEventArgs for the event containing information about the row being bound.</param>
  63.   ''' <remarks></remarks>
  64.   Private Sub theGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles theGridView.RowDataBound
  65.     If (e.Row.FindControl("ddl1") IsNot Nothing) Then
  66.       CType(e.Row.FindControl("ddl1"), DropDownList).DataSource = ddl1Source
  67.       CType(e.Row.FindControl("ddl1"), DropDownList).DataBind()
  68.     End If     
  69.   End Sub
  70.  
  71.   ''' <summary>
  72.   '''  Handles the First DropDownList's SelectedIndexChanged Event.
  73.   '''  Here we check what was selected and generate a DataSource for
  74.   '''  the second DropDownList.
  75.   ''' </summary>
  76.   ''' <param name="sender">The Object that raised the event (the ddl1 DropDownList)</param>
  77.   ''' <param name="e">The EventArgs for the event.</param>
  78.   ''' <remarks></remarks>
  79.   Protected Sub ddl1_OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
  80.     'Grabbing a reference to the ddl1 that raised the event
  81.     Dim ddl1 As DropDownList = CType(sender, DropDownList)
  82.  
  83.     'Grabbing a reference to the GridViewRow that the ddl1 belongs to 
  84.     'so that I can grab a reference to the ddl2 that belongs to that row
  85.     Dim gridViewRowDdlBelongsTo As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
  86.  
  87.     'Grabbing a reference to ddl2 that is in the same row as ddl1
  88.     Dim ddl2 As DropDownList = Nothing
  89.     If gridViewRowDdlBelongsTo IsNot Nothing
  90.       ddl2 = CType(gridViewRowDdlBelongsTo.FindControl("ddl2"), DropDownList)
  91.     End If
  92.  
  93.     'Generating the data source for ddl2 depending on what was selected 
  94.     'in ddl1
  95.     Dim ddl2Source As List(Of ListItem) 'used as the datasource for the second DropDownList in the GridView
  96.     ddl2Source = New List(Of ListItem)
  97.     If ddl1.SelectedIndex Mod 2 = 0 Then
  98.       'generate letters as source for second list
  99.       ddl2Source.Add(New ListItem("A", "1"))
  100.       ddl2Source.Add(New ListItem("B", "2"))
  101.       ddl2Source.Add(New ListItem("C", "3"))
  102.       ddl2Source.Add(New ListItem("D", "4"))
  103.     Else
  104.       'generate roman numerals for source for second list
  105.       ddl2Source.Add(New ListItem("i", "1"))
  106.       ddl2Source.Add(New ListItem("ii", "2"))
  107.       ddl2Source.Add(New ListItem("iii", "3"))
  108.       ddl2Source.Add(New ListItem("iv", "4"))
  109.     End If
  110.  
  111.     'Setting the datasource for ddl2 in the row, and binding to it.
  112.     If ddl2 IsNot Nothing Then
  113.       ddl2.DataSource = ddl2Source
  114.       ddl2.DataBind()
  115.     End If
  116.   End Sub
  117. End Class
Nov 17 '09 #4
Kalkin
6 New Member
Thanks Frinavale, have it working now! I am an idiot and my datasource was empty.

Thanks again
Nov 18 '09 #5
Frinavale
9,735 Recognized Expert Moderator Expert
I'm glad you got it to work :)

-Frinny
Nov 18 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1105
by: Julie Barnet | last post by:
Can someone please post a small example of how to trap a selected index changed event from a dropdownlist in a datagrid? Thanks in advance Julie Barnet
5
13835
by: Steve Jones | last post by:
Hi there, I am working with web forms, and am finding that the standard list of combo box events is dramatically reduced when using the dropdownlist. For example, the only event that I can see...
0
1383
by: DCC700 | last post by:
After converting a web application to 2005, I am receiving an invalid character error when I change the value in a dropdown list. The dropdown list is set to postback on selected index changed and...
0
904
by: jason.hau | last post by:
Anyone come up agains this? Essentially, I've got a custom dropdownlist that inherits from System.Web.UI.WebControls.Dropdownlist (funny that) the only things I'm overriding is rendercontrol in...
0
3710
by: Child X | last post by:
Hi all, I have a web form with two GridViews on it. GridView 'A' populates GridView 'B' based on it 'SelectedRowValue'. What i am after, is ...if someone delete a row from grid 'A' and it is...
1
2486
by: RichGK | last post by:
The first time I run my code the list box sends a selected index changed event even though at this point there has been no activity with the mouse. The value returned from SelectedValue according...
1
2808
by: RK800 | last post by:
could someone help me out with this?? i am creating a dropdown(dd2) dynamically inside a <div> , on the selected index event of another dropdown(dd1). somehow i am not able to handle or get the...
0
2105
by: perumalsamy R | last post by:
I am using gridview in my project.When i am clicking the checkbox ,I want to add the price value on gridview selected index changed event.can you help for my problem please help me?
1
2457
by: kapil tripathi | last post by:
1) Accpet the item in TxtItem in comma seperated values like (Apple, Mango, Orange etc.). Store that item in array object (objArray) in acending order." 2) Create the object of the dropdown...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.