Solved

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

Posted on 2011-03-10
9
563 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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