Solved

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

Posted on 2011-03-10
9
599 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

759 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