Solved

Filter gridview database with multiple CheckBoxLists?

Posted on 2011-03-25
4
891 Views
Last Modified: 2012-05-11
Hello,
one of experts-exchange member, Tuyau2poil,  has help me, and give this code to filter gridview column with two, or more checkboxes in one CheckBoxList1

This code is working only to filter one column FirstName with CheckBoxList1. How can I add also another CheckBoxList2 to filter column SecondName, and for example third  CheckBoxList3 to filter third column City????

dim MyFilter as string = ""
Dim i As Integer
        For i = 0 To CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected Then
      If MyFilter = "" then 
                 MyFilter = " WHERE FIRSTNAME like '%" & CheckBoxList1.Items(i).Text & "%'"
      Else
                 MyFilter = MyFilter & " OR FIRSTNAME like '%" & CheckBoxList1.Items(i).Text & "%'"
      End If           
           End If
        Next
SqlDataSource1.SelectCommand = "SELECT * FROM MAIN " & MyFilter

Open in new window


Thank you in advance for help.
0
Comment
Question by:dejandejanovic
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
kswathi earned 500 total points
ID: 35214246
Dim MyFilter As String = ""
            Dim Count As Integer = 0
            Dim i As Integer
            Dim j As Integer
            For i = 0 To CheckBoxList1.Items.Count - 1
                If CheckBoxList1.Items(i).Selected Then
                    Count += 1
                    If MyFilter = "" Then
                        MyFilter = " WHERE FIRSTNAME like '%" & CheckBoxList1.Items(i).Text & "%'"
                    Else
                        MyFilter = MyFilter & " OR FIRSTNAME like '%" & CheckBoxList1.Items(i).Text & "%'"
                    End If
                End If
            Next

            For j = 0 To CheckBoxList2.Items.Count - 1
                If CheckBoxList2.Items(j).Selected Then
                    If Count = 0 Then
                        MyFilter = " WHERE LASTNAME like '%" & CheckBoxList2.Items(j).Text & "%'"
                    Else
                        MyFilter = MyFilter & " OR LASTNAME like '%" & CheckBoxList2.Items(j).Text & "%'"
                    End If

                End If

            Next

0
 

Author Comment

by:dejandejanovic
ID: 35217401
Thanks Kswathi for reply, help.

Hm, I tryed in the same why, but still your code is a little different.

I will try it, and let you know...
0
 

Author Closing Comment

by:dejandejanovic
ID: 35223383
Works perfect.
0
 

Author Comment

by:dejandejanovic
ID: 35223394
With just a little modification of upper code, I have add below code, if someone might need it.
Also included third CheckboxList3.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyFilter As String = ""
        Dim Count As Integer = 0
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        For i = 0 To CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected Then
                Count += 1
                If MyFilter = "" Then
                    MyFilter = " WHERE FIRSTNAME like '%" & CheckBoxList1.Items(i).Text & "%'"
                Else
                    MyFilter = MyFilter & " OR FIRSTNAME like '%" & CheckBoxList1.Items(i).Text & "%'"
                End If
            End If
        Next

        For j = 0 To CheckBoxList2.Items.Count - 1
            If CheckBoxList2.Items(j).Selected Then
                Count += 1
                If MyFilter = "" Then
                    MyFilter = " WHERE SECONDNAME like '%" & CheckBoxList2.Items(j).Text & "%'"
                Else
                    MyFilter = MyFilter & " OR SECONDNAME like '%" & CheckBoxList2.Items(j).Text & "%'"
                End If
            End If


        Next

        For k = 0 To CheckBoxList3.Items.Count - 1
            If CheckBoxList3.Items(k).Selected Then
                Count += 1
                If MyFilter = "" Then
                    MyFilter = " WHERE CITY like '%" & CheckBoxList3.Items(k).Text & "%'"
                Else
                    MyFilter = MyFilter & " OR CITY like '%" & CheckBoxList3.Items(k).Text & "%'"
                End If
            End If
        Next
        SqlDataSource1.SelectCommand = "SELECT * FROM MAIN " & MyFilter
    End Sub

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question