Gridview Select one dropdownlist to filter the 2nd dropdownlist

I have a gridview with two dropdownlists in it.
When a user selects the first dropdown I would like the 2nd dropdown list to only display associated records.
I am using a separate linqdatasource for each dropdown.
How do I get the selectedvalue from the first dropdown and apply to the 2nd?

Thanks
SheritlwAsked:
Who is Participating?
 
informaniacConnect With a Mentor Commented:
Try this code
<asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList ID="d1" runat=server 
        onselectedindexchanged="d1_SelectedIndexChanged"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>

In code behind
protected void d1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = ((DropDownList)sender);
            //To get the row
            GridViewRow gvRow = ((GridViewRow)ddl.Parent.Parent);
        }

Open in new window

0
 
Rajkumar GsSoftware EngineerCommented:
I think, informaniac is almost complete.

Enable 'AutoPostBack' property of the DropDownList 'd1' to True.

Then in SelectedIndexChanged event write code to identity the two dropdownlist controls. Then as usual bind the second dropdownlist based on the SelectedItem.Value of the first dropdownlist control

Raj
0
 
ddayx10Commented:
Im sure there's more than one way, but I put together a quick sample of one way to go about it. Just real simple for demo purposes:

1) Setup gridview with 2 dropdownlists
  a) 1st dropdown affects second using autopostback and onselectedindexchanged event
2) In onselectedindexchanged event get the 2 dropdowns, change dropdown items of ddl2 based on choice from ddl1.

I realize you have datasources to bind to etc, but the premise is the same just have to flush it out to work with your details. The code runs stand alone if you want to check it out. I kept it really simple for readablility.
****ASPX PAGE****
    <asp:GridView ID="Gv1" runat="server" AutoGenerateColumns="false">
		<Columns>
			<asp:TemplateField>
				<ItemTemplate>
					<span><%#Eval("text")%></span>
					<asp:DropDownList ID="Ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Ddl1_SelectedIndexChanged">
						<asp:ListItem Text="Option1" Value="0" />
						<asp:ListItem Text="Option2" Value="1" />
					</asp:dropdownlist>
					<asp:DropDownList ID="Ddl2" runat="server" Enabled="false">
						<asp:ListItem Text="AutoUpdated" Value="0" />
					</asp:DropDownList>
				</ItemTemplate>
			</asp:TemplateField>	
		</Columns>
    </asp:GridView>

****"ASPX.VB PAGE****
Partial Class GridDynamicDDLs
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim _GridList As New ListItemCollection

        If Not IsPostBack Then
            'Just need something to bind to
            Dim _BindingList As New ListItemCollection
            _BindingList.Add(New ListItem("row1", "0"))
            _BindingList.Add(New ListItem("row2", "1"))
            _BindingList.Add(New ListItem("row3", "2"))
            Gv1.DataSource = _BindingList
            Gv1.DataBind()
        End If

    End Sub

    Protected Sub Ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddl1 As DropDownList = sender
        Dim ddl2 As DropDownList = sender.parent.findcontrol("ddl2")
        ddl2.Items.Clear()
        ddl2.Enabled = True

        'You would assign your datasources here.
        Select Case ddl1.SelectedValue
            Case 0
                ddl2.Items.Add(New ListItem("X1", "0"))
                ddl2.Items.Add(New ListItem("X2", "1"))
                ddl2.Items.Add(New ListItem("X3", "2"))
            Case 1
                ddl2.Items.Add(New ListItem("Y1", "0"))
                ddl2.Items.Add(New ListItem("Y2", "1"))
                ddl2.Items.Add(New ListItem("Y3", "2"))
        End Select
    End Sub
End Class

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ddayx10Commented:
Ah dude got there in front of me :)
0
 
SheritlwAuthor Commented:
I agree.  I need to also find the 2nd dropdownlist.
I have added the code provided by informaniac, but it does not show how to get the 2nd dropdown.

I provided an example below.

Thanks
Protected Sub ddProdType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        'To get the row
        Dim gvRow As GridViewRow = DirectCast(ddl.Parent.Parent, GridViewRow)
        If ddl.SelectedItem.Value > -1 Then
            Dim i As Integer = ddl.SelectedValue
            ''I need the other control to change it's source
        End If
    End Sub

Open in new window

0
 
ddayx10Commented:
His is very similar to mine. To find the second dropdown you would simple use findcontrol on the gvRow you found.

dim ddl2 as dropdownlist = gvRow.findcontrol("idof2nddropdown")

Or if this doesn't work then you can change it a little:
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        'To get the row
        Dim ddlParent As DataControlFieldCell = DirectCast(ddl.Parent, DataControlFieldCell)
        If ddl.SelectedItem.Value > -1 Then
            Dim i As Integer = ddl.SelectedValue
            ''I need the other control to change it's source
            Dim ddl2 As DropDownList = ddlParent.FindControl("IDOfDDL2")
        End If
0
 
ddayx10Commented:
BTW I believe both work.
0
 
SheritlwAuthor Commented:
I have attached what I now have.  
I am getting an error when trying to bind my linq select to my linqdatasource.
The error is weird
Could not find a property or field called 'SELECT [t0].[LUProdSellUseID], [t0].[ProductName], [t0].[UserID], [t0].[ProductTypeID], [t0].[ProdSell]
FROM [dbo].[vProductNameForSale] AS [t0]
WHERE ((CONVERT(NVarChar(MAX),[t0].[UserID])) = @p0) AND ([t0].[ProductTypeID] = @p1)
' on the data context type 'SalonDataClassesDataContext' of LinqDataSource 'LnqProductNames'.


How do I re-bind my linq select to the linqdatasource?

Thanks


Protected Sub ddProdType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        'To get the row
        Dim gvRow As GridViewRow = DirectCast(ddl.Parent.Parent, GridViewRow)
        Dim ddlParent As DataControlFieldCell = DirectCast(ddl.Parent, DataControlFieldCell)
        If ddl.SelectedItem.Value > -1 Then
            Dim i As Integer = ddl.SelectedValue
            ''I need the other control to change it's source
            Dim ddl2 As DropDownList = ddlParent.FindControl("ddProductSale")
            If Not ddl2 Is Nothing Then
                Dim dc As New SalonDataClassesDataContext
                Dim u As New SalonUtilities
                Dim sUser As String = u.CheckSessionAndReturnUser
                Dim q = (From a In dc.vProductNameForSales Where a.UserID.ToString = sUser _
                         And a.ProductTypeID = i Select a)
                If Not q Is Nothing Then
                    'Assign q to my linqdatasource
??????
                    Me.LnqProductNames.
                End If
            End If
        End If

    End Sub

Open in new window

0
 
ddayx10Commented:
I got nuthin...

I thought u wanted to add/change the datasource for the 2nd ddl?

ddl2.datasource = somedatabindableobject (maybe your linq src)
ddl2.databind

I haven't played with Linq in a long while, never was very good with it. I know it does some "unconventional" stuff but the nature of your error makes me think the issue is in your line 14 and the way you are concatenating sUser but I'm just punting!
0
 
Alpesh PatelAssistant ConsultantCommented:
Hi,

Use linqDAtasource and use selectedvalue of Dropdown as paramerter.
0
 
SheritlwAuthor Commented:
I use a linqdatasource in the aspx page, but when the column gets filtered, I need to know how to rebind the linqdatasource.

Thanks
0
 
ddayx10Connect With a Mentor Commented:
If the ddl has a datasource, then you are changing a property (filtering) of that datasource, you should re-bind the ddl by just calling ddl.databind()... Whatever changes you made to the datasource will be reflected.

The issue seems to be more about how to filter/utilize the linq filtring properly. It is really hard to know how to proceed or even setup a sample when you are working with custom objects that we cannot see.

I think this issue is beginning to suffer from a bit of scope creep. You might get better play by re-packaging the issue as a linq question at this stage of the game. People who have insight are going to see the original issue and never look at the current problem because of this.
0
 
SheritlwAuthor Commented:
I tried to find a programming selection for  Linq, but with no luck.  It is supposed to be in the same area as asp.net and vb.net.
Trying to use filtering within the gridview is take to much time to figure out.
I am going to take that out and try to do it later.
Thank you for your help.






0
All Courses

From novice to tech pro — start learning today.