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 21002 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 that will trigger when a
user selects a different menu item is 'selected index changed'. I can
confirm with a separate button that the index has in fact changed
after a pulldown selection takes place, yet the internal 'selected
index changed'...
|
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 perform some
actions. The postback is not happening due to this error on the page.
All the dropdown lists on this page cause this error, but I use
dropdowns on many other pages that do not have any problems. Any
ideas???
*** Sent via...
|
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 taht it adds an image prior to
base.rendercontrol. For some reason, if autopostback is set and the
listitems are added progrommatically, the items aren't saved in the
controlstate and the viewstate doesn't keep the selectedindex value.
Have i...
|
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
currently the 'SelectedRow'...
I want to decrement GridViews selected index by 1 so it falls to the
| |
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 to the
debugger is System.Data.DataRowView.
How can I either stop this initial selected index changed event from
happening automatically or alternatively what can I check for so that
I can avoid executing further code if the selected value is...
|
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 selectedindexchange event of dd2. i get the values inside dd2, but does not respond to any event. i am using
the code is as below.
Function getdatafromdb(ByVal s As String) As String
Try
|
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 list objDropDwon on runtime and fill its items from objArray."
3) Create server side table and
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |