Solved

Filter gridview database with multiple CheckBoxLists?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now