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
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: -
Protected Sub FirstDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles FirstDropDownList.SelectedIndexChanged
-
-
'Grabbing a reference to the row that the DropDownList belongs to:
-
Dim gridViewRowDdlBelongsTo As GridViewRow = Ctype( Ctype(sender,Control).NamingContainer, GridViewRow)
-
-
'Grabbing a reference to the second DropDownList in the row:
-
Dim secondDropDownList As DropDownList = CType(gridViewRowDdlBelongsTo.FindControl("secondDropDownList"),DropDownList)
-
-
'Now you just need to set the data source for the secondDropDownList
-
'according to the selection in the firstDropDownList
-
'.......
-
End Sub
-
C#: -
protected void FirstDropDownList_SelectedIndexChanged(object sender, EventArgs e) {
-
// Grabbing a reference to the row that the DropDownList belongs to:
-
GridViewRow gridViewRowDdlBelongsTo = (GridViewRow)(((Control)sender).NamingContainer);
-
-
// Grabbing a reference to the second DropDownList in the row:
-
DropDownList secondDropDownList = (DropDownList)gridViewRowDdlBelongsTo .FindControl("secondDropDownList");
-
-
// Now you just need to set the data source for the secondDropDownList
-
// according to the selection in the firstDropDownList
-
// ....
-
}
-Frinny
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. - Protected Sub ddMerchCat_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
-
Dim objRecordsource As New Bestvest_DataAccess.cDataAccess
-
Dim dstSubMerch As New DataSet
-
Dim strMerch As String
-
Dim ddMerchCat As DropDownList = CType(gvRentalIncome.Rows(0).FindControl("ddMerchCat"), DropDownList)
-
-
Dim drwSubMerch As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
-
-
strMerch = Replace(ddMerchCat.SelectedItem.Text, " ", "")
-
dstSubMerch = objRecordsource.getRecordSet("Select * from tbl_Merc_" & strMerch & "")
-
-
Dim ddSubMerch As DropDownList = CType(drwSubMerch.FindControl("ddSubMerchCat"), DropDownList)
-
ddSubMerch.DataSource = dstSubMerch
-
ddSubMerch.DataValueField = "" & strMerch & "ID"""
-
ddSubMerch.DataTextField = "Type"
-
ddSubMerch.DataBind()
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): -
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="MyNamespace._Default" %>
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head runat="server">
-
<title>Testing DropDownLists</title>
-
</head>
-
<body>
-
<asp:GridView ID="theGridView" runat="server" AutoGenerateColumns="false">
-
<Columns>
-
<asp:TemplateField HeaderText="First DropDownList" ItemStyle-HorizontalAlign="Center">
-
<ItemTemplate> <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl1_OnSelectedIndexChanged"></asp:DropDownList></ItemTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="Second DropDownList" ItemStyle-HorizontalAlign="Center">
-
<ItemTemplate><asp:DropDownList ID="ddl2" runat="server"></asp:DropDownList></ItemTemplate>
-
</asp:TemplateField>
-
</Columns>
-
</asp:GridView>
-
</form>
-
</body>
-
</html>
And here is my very simple VB code: -
Partial Public Class _Default
-
Inherits System.Web.UI.Page
-
-
Private src As DataView 'used as the datasource for the GridView
-
Private ddl1Source As List(Of ListItem) 'used as the datasource for the first DropDownList in the GridView
-
-
-
''' <summary>
-
''' Creates a simple data source to be used in the GridView
-
''' </summary>
-
''' <remarks></remarks>
-
Private Sub GenerateSource()
-
'Only creating a new source if one doesn't already exist in session.
-
If Session("src") Is Nothing Then
-
Dim tbl As New DataTable
-
tbl.Columns.Add("ID")
-
tbl.Columns.Add("first")
-
tbl.Columns.Add("second")
-
-
For i As Integer = 1 To 10
-
Dim r As DataRow = tbl.NewRow()
-
r("ID") = i.ToString
-
tbl.Rows.Add(r)
-
Next
-
src = New DataView(tbl)
-
Session("src") = src
-
-
'Generating the default source for the first DropDownList:
-
ddl1Source = New List(Of ListItem)
-
For i As Integer = 1 To 10
-
ddl1Source.Add(New ListItem(i.ToString, i.ToString))
-
Next
-
Session("ddl1Source") = ddl1Source
-
Else
-
src = CType(Session("src"), DataView)
-
ddl1Source = CType(Session("ddl1Source"), List(Of ListItem))
-
End If
-
End Sub
-
-
''' <summary>
-
''' Handles the Page PreRender event. This happens just before the
-
''' the page's controls are rendered as HTML and sent to the browser.
-
''' </summary>
-
''' <param name="sender">The Object that raised the event (the Page).</param>
-
''' <param name="e">The EventArgs for the event.</param>
-
''' <remarks></remarks>
-
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
-
If IsPostBack = False Then
-
'If it's the first time loading the page, generating the data source
-
'for the GridView and binding to it.
-
GenerateSource()
-
theGridView.DataSource = src
-
theGridView.DataBind()
-
End If
-
End Sub
-
-
''' <summary>
-
''' Handles theGridView's RowDataBound Event. Here I am setting the
-
''' data source for the first DropDownList and binding it to it.
-
''' </summary>
-
''' <param name="sender">The Object that raised the event (theGridView).</param>
-
''' <param name="e">The GridViewRowEventArgs for the event containing information about the row being bound.</param>
-
''' <remarks></remarks>
-
Private Sub theGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles theGridView.RowDataBound
-
If (e.Row.FindControl("ddl1") IsNot Nothing) Then
-
CType(e.Row.FindControl("ddl1"), DropDownList).DataSource = ddl1Source
-
CType(e.Row.FindControl("ddl1"), DropDownList).DataBind()
-
End If
-
End Sub
-
-
''' <summary>
-
''' Handles the First DropDownList's SelectedIndexChanged Event.
-
''' Here we check what was selected and generate a DataSource for
-
''' the second DropDownList.
-
''' </summary>
-
''' <param name="sender">The Object that raised the event (the ddl1 DropDownList)</param>
-
''' <param name="e">The EventArgs for the event.</param>
-
''' <remarks></remarks>
-
Protected Sub ddl1_OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
-
'Grabbing a reference to the ddl1 that raised the event
-
Dim ddl1 As DropDownList = CType(sender, DropDownList)
-
-
'Grabbing a reference to the GridViewRow that the ddl1 belongs to
-
'so that I can grab a reference to the ddl2 that belongs to that row
-
Dim gridViewRowDdlBelongsTo As GridViewRow = CType(CType(sender, Control).NamingContainer, GridViewRow)
-
-
'Grabbing a reference to ddl2 that is in the same row as ddl1
-
Dim ddl2 As DropDownList = Nothing
-
If gridViewRowDdlBelongsTo IsNot Nothing
-
ddl2 = CType(gridViewRowDdlBelongsTo.FindControl("ddl2"), DropDownList)
-
End If
-
-
'Generating the data source for ddl2 depending on what was selected
-
'in ddl1
-
Dim ddl2Source As List(Of ListItem) 'used as the datasource for the second DropDownList in the GridView
-
ddl2Source = New List(Of ListItem)
-
If ddl1.SelectedIndex Mod 2 = 0 Then
-
'generate letters as source for second list
-
ddl2Source.Add(New ListItem("A", "1"))
-
ddl2Source.Add(New ListItem("B", "2"))
-
ddl2Source.Add(New ListItem("C", "3"))
-
ddl2Source.Add(New ListItem("D", "4"))
-
Else
-
'generate roman numerals for source for second list
-
ddl2Source.Add(New ListItem("i", "1"))
-
ddl2Source.Add(New ListItem("ii", "2"))
-
ddl2Source.Add(New ListItem("iii", "3"))
-
ddl2Source.Add(New ListItem("iv", "4"))
-
End If
-
-
'Setting the datasource for ddl2 in the row, and binding to it.
-
If ddl2 IsNot Nothing Then
-
ddl2.DataSource = ddl2Source
-
ddl2.DataBind()
-
End If
-
End Sub
-
End Class
Thanks Frinavale, have it working now! I am an idiot and my datasource was empty.
Thanks again
Frinavale 9,735
Recognized Expert Moderator Expert
I'm glad you got it to work :)
-Frinny
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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?
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |