Solved

Filter gridview database with multiple CheckBoxLists?

Posted on 2011-03-25
4
893 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…

732 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