Solved

Filter gridview database with multiple CheckBoxLists?

Posted on 2011-03-25
4
892 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Title # Comments Views Activity
Winform Module - What is the ASP.Net equiv 2 38
Generate Numbers in JQuery file 11 72
VB.NET Inline If statement 4 38
Vb.net progressbar with timer1 in visual studio win app 11 34
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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