Solved

Filter gridview database with multiple CheckBoxLists?

Posted on 2011-03-25
4
888 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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