Results 1 to 2 of 2

Thread: Filter Grid View with 2 drop down menus

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2016
    Posts
    4

    Filter Grid View with 2 drop down menus

    I have a simple gridview that needs to shows the facilities in our system. The Gridview also needs to be filtered by two dropdown menus.
    • Menu 1: facility type
    • Menu 2: facility status


    I can get the gridview to bind on load and I can get the gridview to bind one dropdown menu after update control but I canít get it to do both. I need to get it to filter on whichever dropdown menu is being selected, either by facility or by status.

    Iím not passing the value from the dropdown list properly, how do I do this?
    Code:
    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
    
            Dim srtfacType As String = Me.DropDownList1.SelectedValue
    
            If IsPostBack Then
                Me.GridView1.DataSource = sqlFacType
                Me.GridView1.DataBind()
            End If
    
            'If Me.DropDownList1.Text = facType Then
            'Me.GridView1.DataSource = SqlDataSource1
            'Me.GridView1.DataBind()
            'End If
        End Sub
    
    Protected Sub DropDownList2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList2.SelectedIndexChanged
            Dim srtfacStatus As String = Me.DropDownList2.SelectedValue
    
            If IsPostBack Then
                Me.GridView1.DataSource = Sql
                Me.GridView1.DataBind()
            End If
        End Sub
    
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"></asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HDOConnectionString %>" SelectCommand="SELECT DISTINCT [Facility_Name], [Facility_Type], [Location], [ContactPerson], [Status], [Phone], [City]  FROM [FacilityLocations] WHERE ([Facility_Type] = @Facility_Type) ORDER BY [Facility_Type]">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList1" Name="Facility_Type" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:HDOConnectionString %>" SelectCommand="SELECT DISTINCT [Facility_Name], [Facility_Type], [Location], [ContactPerson], [Status], [Phone], [City] FROM [FacilityLocations] ORDER BY [Facility_Name]"></asp:SqlDataSource>
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                Me.GridView1.DataSource = SqlDataSource2
                Me.GridView1.DataBind()
            End If
        End Sub
    Any help is appreciated, thanks.

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    1,998

    Re: Filter Grid View with 2 drop down menus

    Quote Originally Posted by kawi6rr View Post
    I have a simple gridview that needs to shows the facilities in our system. The Gridview also needs to be filtered by two dropdown menus.
    • Menu 1: facility type
    • Menu 2: facility status


    I can get the gridview to bind on load and I can get the gridview to bind one dropdown menu after update control but I canít get it to do both. I need to get it to filter on whichever dropdown menu is being selected, either by facility or by status.

    Iím not passing the value from the dropdown list properly, how do I do this?
    Code:
    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
    
            Dim srtfacType As String = Me.DropDownList1.SelectedValue
    
            If IsPostBack Then
                Me.GridView1.DataSource = sqlFacType
                Me.GridView1.DataBind()
            End If
    
            'If Me.DropDownList1.Text = facType Then
            'Me.GridView1.DataSource = SqlDataSource1
            'Me.GridView1.DataBind()
            'End If
        End Sub
    
    Protected Sub DropDownList2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList2.SelectedIndexChanged
            Dim srtfacStatus As String = Me.DropDownList2.SelectedValue
    
            If IsPostBack Then
                Me.GridView1.DataSource = Sql
                Me.GridView1.DataBind()
            End If
        End Sub
    
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"></asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HDOConnectionString %>" SelectCommand="SELECT DISTINCT [Facility_Name], [Facility_Type], [Location], [ContactPerson], [Status], [Phone], [City]  FROM [FacilityLocations] WHERE ([Facility_Type] = @Facility_Type) ORDER BY [Facility_Type]">
                <SelectParameters>
                    <asp:ControlParameter ControlID="DropDownList1" Name="Facility_Type" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:HDOConnectionString %>" SelectCommand="SELECT DISTINCT [Facility_Name], [Facility_Type], [Location], [ContactPerson], [Status], [Phone], [City] FROM [FacilityLocations] ORDER BY [Facility_Name]"></asp:SqlDataSource>
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                Me.GridView1.DataSource = SqlDataSource2
                Me.GridView1.DataBind()
            End If
        End Sub
    Any help is appreciated, thanks.
    Instead of using SqlDataSource control to bind data to a GridView control, I would suggest a better option which is to retrieve data from the database using sql statements, use the dropdown's selected value as the parameter of the sql statement and store the retrieved information to a List or DataTable object. The List and DataTable objects will be assigned to the GridView's DataSource property.

    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width