Filter data in grid view on a web form

I am new to ASP.net programming.   Can someone show me a example on how to filter data in my grid view.  I currently have a listbox where users would select one or multiple items  I would like the gridview to then show only data realated to the items that the user selected.

Thank You
bruinfan77Asked:
Who is Participating?
 
nmarunConnect With a Mentor Commented:
You need to remove the items from listbox1 in a separate loop as this causes referencing issues

Here's my aspx:

        <asp:ListBox ID="listbox1" runat="server" SelectionMode="Multiple" />
        <br />
        <asp:ListBox ID="listbox2" runat="server" SelectionMode="Multiple" />

The code below is the code-behind in the btnAdd_Click event.

    For i As Integer = 0 To listbox1.Items.Count - 1
        If listbox1.Items(i).Selected Then
            listbox2.Items.Add(listbox1.Items(i))
        End If
    Next
    
    For i As Integer = listbox1.Items.Count - 1 To 0 Step -1
        If listbox1.Items(i).Selected Then
            listbox1.Items.Remove(listbox1.Items(i))
        End If
    Next

Open in new window

0
 
nmarunCommented:
Set the AutoPostback property of your listbox to 'true' and in the <listbox>_SelectedIndexChanged event, set your SELECT statement to something like:

"SELECT * from TABLE1 where col1 = " + listbox1.SelectedText;

and re-bind to the gridview.
0
 
nmarunCommented:
bruinfan77, were you able to get this to work?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
bruinfan77Author Commented:
I partially have it working.  If I only use listbox1 then I my gridview contains the correct data.  However I am passing the selected items from listbox1 to listbox2.  And it is the items that are in listbox2 that I want to use as my parameter for the gridview.    Currently Listbox2 is unbounded.  I do not really know what I should bound it to. Below is the code that works with listbox1.
Partial Class _Default
    Inherits System.Web.UI.Page
 
 
    Sub Preselect_ListBox(ByVal Src As Object, ByVal Args As EventArgs)
 
        ListBox1.Items(0).Selected = True
 
    End Sub
 
    Sub Get_Product_Types(ByVal Src As Object, ByVal Args As EventArgs)
 
        Dim SQLString As String = "SELECT PROP_TYPE, LIST_NO, LIST_PRICE, STREET_NO, STREET_NAME, TOWN_NUM, [AREA], ZIP_CODE FROM idx_mf WHERE "
 
        Dim Item As ListItem
        For Each Item In ListBox1.Items
            If Item.Selected Then
                SQLString &= " Town_NUM = '" & Item.Value & "' OR "
            End If
        Next
 
        SQLString = Left(SQLString, Len(SQLString) - 4)
        SqlDataSource1.SelectCommand = SQLString
 
    End Sub
 
 
    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        While ListBox1.Items.Count > 0 AndAlso ListBox1.SelectedItem IsNot Nothing
            Dim selectedItem As ListItem = ListBox1.SelectedItem
            selectedItem.Selected = False
            ListBox2.Items.Add(selectedItem)
 
            ListBox1.Items.Remove(selectedItem)
        End While
 
        '  Call SortSlctBox()
    End Sub
 
    Protected Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        While ListBox2.Items.Count > 0 AndAlso ListBox2.SelectedItem IsNot Nothing
            Dim selectedItem As ListItem = ListBox2.SelectedItem
            selectedItem.Selected = False
            ListBox1.Items.Add(selectedItem)
            ListBox2.Items.Remove(selectedItem)
 
        End While
 
 
        'Call SortListBox()
 
    End Sub
End Class

Open in new window

0
 
nmarunCommented:
Trying to get the big picture:

You have two listboxes that you want bound to a gridview. So a user selects an item from listbox1 and then selects an item from listbox2, for which you want to show filtered results in the gridview?

Is this correct?
0
 
bruinfan77Author Commented:
No.  I have two listboxes.  ListBox1 contains the data and ListBox2 start life empty until the user adds items from listbox1 to listbox2.  The gridview need to be filtered based on the item in ListBox2.
0
 
nmarunCommented:
Oh ok... Your code is pretty close to achieving this. Try this:

In your btnAdd_Click subroutine, call the Get_Product_Types subroutine and change your Get_Product_Types subroutine to the following:

    Sub Get_Product_Types(ByVal Src As Object, ByVal Args As EventArgs)
 
        Dim SQLString As String = "SELECT PROP_TYPE, LIST_NO, LIST_PRICE, STREET_NO, STREET_NAME, TOWN_NUM, [AREA], ZIP_CODE FROM idx_mf WHERE "
 
        Dim Item As ListItem
        For Each Item In ListBox2.Items
            SQLString &= " Town_NUM = '" & Item.Value & "' OR "
        Next
 
        SQLString = Left(SQLString, Len(SQLString) - 4)
        SqlDataSource1.SelectCommand = SQLString
 
    End Sub

Open in new window

0
 
nmarunCommented:
In short, you add the selected items of listbox1 to listbox2 and then call the Get_Product_Types subroutine to read all the items of listbox2 and set the select command.
0
 
bruinfan77Author Commented:
I tried that before but I was getting an error.  argument not specified for parameter 'Args'
0
 
nmarunCommented:
What subroutine throws this error?
0
 
bruinfan77Author Commented:
btnAdd_Click
0
 
bruinfan77Author Commented:
Thank You very much for your help that worked perfectly.  I very new to ASP.net programming.  I was also able to determine why my original code was not working.  It was becasue I stated that the items in ListBox2 needed to be selected. Before it would update grid view.
0
 
nmarunCommented:
Good to know it worked out fine for you.
0
All Courses

From novice to tech pro — start learning today.