Solved

Filter gridview column with multiple comboxbox.text. Need someone to check code.

Posted on 2011-03-10
9
580 Views
Last Modified: 2012-05-11
Hello experts,

I have a gridview with columns id, firstname, secondname. I have write below vb.net code to filter FirstName column with two or more checkboxes. I have add two checkboxes to try filter it.
Column values are:
John
James
Paul

Checkbox1 value  is "John".
Checkbox2 value is "James"

 The code is working, but not as I want to. Gridview is filtered if only  one checkbox is checked. But, I want if both checkboxes are check that I get rows which contains "John", and "James" values.
But, I also think that I'm doing something wrong, not the right way!?

SqlDataSource1.SelectCommand = "SELECT * FROM MAIN WHERE FIRSTNAME like '%" & CheckBoxList1.Text & "%' and FIRSTNAME like '%" & CheckBoxList2.Text & "%'"
        SqlDataSource1.DataBind()

Open in new window


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

Expert Comment

by:Tuyau2poil
ID: 35101521
the problem is that you don't check if checkbox is checked or not.
secundly, checkbox text property is constant and doesn't rely with checked state.

so, to end that explication, this is a possible query :

SqlDataSource1.SelectCommand = "SELECT * FROM MAIN WHERE ( FIRSTNAME like '%" & CheckBoxList1.Text & "%'  AND " & CheckBoxList1.checked & ")  OR (FIRSTNAME like '%" & CheckBoxList2.Text & "%' AND " & CheckBoxList2.checked & ")"
        SqlDataSource1.DataBind()

example : ck1 checked and ck2 not checked :
the query is SELECT * FROM MAIN WHERE (FIRSTNAME = PAUL and 1) OR (FIRSTNAME = ALAN and 0)
that's equal to : SELECT * FROM MAIN WHERE (FIRSTNAME = PAUL and 1) OR 0
after boolean simplify :  SELECT * FROM MAIN WHERE FIRSTNAME = PAUL

hope my french translated will help you !
0
 

Author Comment

by:dejandejanovic
ID: 35111940
Hi, and thanks for reply.

I have try with your solution, but getting this error as printscreen show.

Please advise, thanks.
printscreen.jpg
0
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35113499
oups I read "checkbox" and that's checkboxlist ! sorry for that mistake :

you must iterate thru items in checkboxlist


Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
           
 Dim i As Integer

         For i=0 To checkboxlist1.Items.Count - 1

            If checkboxlist1.Items(i).Selected Then

               SqlDataSource1.SelectCommand = "SELECT * FROM MAIN WHERE FIRSTNAME like '%" &  checkboxlist1.Items(i).Text

            End If

         Next

end sub
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35113519
and don't forget to put onselectedindexchanged="CheckBoxList1_SelectedIndexChanged" in aspx tags like this :

 <asp:CheckBoxList ID="CheckBoxList1" runat="server"
            onselectedindexchanged="CheckBoxList1_SelectedIndexChanged" >
            <asp:ListItem>toto</asp:ListItem>
            <asp:ListItem>titi</asp:ListItem>
            <asp:ListItem>tata</asp:ListItem>
        </asp:CheckBoxList>

have a good WE !
0
 

Author Comment

by:dejandejanovic
ID: 35131840
Hm, the code is working, but not as it should. It filter gridview, but only by one item, it return rows only from one value. But, still I would like if  I have 5 items, that it would return if select 2 of them to return rows of both values.

By the way, thanks for helping.

   Dim i As Integer

        For i = 0 To CheckBoxList1.Items.Count - 1

            If CheckBoxList1.Items(i).Selected Then

                SqlDataSource1.SelectCommand = "SELECT * FROM MAIN WHERE FIRSTNAME like '%" & CheckBoxList1.Items(i).Text & "%'"

            End If

        Next

    End Sub

Open in new window

0
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35175647
I was in hollidays sorry : I will reply tomorrow ! thanks !
0
 
LVL 4

Accepted Solution

by:
Tuyau2poil earned 500 total points
ID: 35183476
OK, you can add 'OR' statements to query like this :

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

End Sub

SqlDataSource1.SelectCommand = "SELECT * FROM MAIN " & MyFilter
0
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35183522
PS : obviously 'End sub' must be at end of proc. (misplaced in my sample).
0
 

Author Closing Comment

by:dejandejanovic
ID: 35184345
Heheheh, ti works. Now it works perfectly, also if I add another Item.

Tuyau2poil, thanks million times. I have also learned a lot from this post.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

810 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