Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-10
9
Medium Priority
?
611 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Screencast - Getting to Know the Pipeline

971 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