Link to home
Start Free TrialLog in
Avatar of dejandejanovic
dejandejanovic

asked on

How to filter gridview with checkboxlist if select 2 or more checkboxes?!

Hello,

I need help.
I'm a begginer, a student, and still learning asp.net with vb.net. I have stucked at filtering gridview with ComboBoxList as multiple checkboxes, if selected more then 2.
I have add a gridview with all columns from database (id, firstname, secondname), and assigned Comboboxlist control to gridview. It is working, but only if one checkbox is selected. But I want to retrieve, also rows if second, or third checkbox is selected.

SELECT * FROM [main] WHERE ([FirstName] = ?)

Open in new window


Thank you in advance for help to anyone.
printscren1.jpg
printscren2.jpg
SOLUTION
Avatar of joshbula
joshbula

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A code like below will work for you in case of multiple items selected.

CODE EXAMPLE
==============
Dim mySqlQuery as String = "SELECT * FROM [main] WHERE [FirstName] IN "

Dim strchklist As String = ""
     Dim li As ListItem
     For Each li In CheckBoxList1.Items
          If li.Selected Then
               strchklist += li.Text + " "
          End If
     Next

     If strchklist = "" Then
          Response.Write("No item Selected")
     Else
          mySqlQuery = mySqlQuery + " (" + strchklist + ")"
          ' EXECUTE YOUR QUERY HERE.
     End If


Hope this helps.
-- UPDATED VERSION of solution.

A code like below will work for you in case of multiple items selected.

CODE EXAMPLE
==============
      Dim csvOfSelectedItems As String = ""
      Dim li As ListItem
      For Each li In CheckBoxList1.Items
            If li.Selected Then
                  csvOfSelectedItems += li.Text + ","
            End If
      Next

      If csvOfSelectedItems = "" Then
            Response.Write("No item Selected")
      Else
            ' Trim the last comma char
            csvOfSelectedItems = csvOfSelectedItems.Substring(1, csvOfSelectedItems.Length - 1)

            ' Prepare Query string
            Dim sqlQuery as String = "SELECT * FROM [main] WHERE [FirstName] IN (" + csvOfSelectedItems + ")"

            ' EXECUTE sqlQuery HERE and use the results.
      End If


Hope this helps.
Dim csvOfSelectedItems As String = ""
	Dim li As ListItem
	For Each li In CheckBoxList1.Items
		If li.Selected Then
			csvOfSelectedItems += li.Text + ","
		End If
	Next

	If csvOfSelectedItems = "" Then
		Response.Write("No item Selected")
	Else
		' Trim the last comma char
		csvOfSelectedItems = csvOfSelectedItems.Substring(1, csvOfSelectedItems.Length - 1)

		' Prepare Query string
		Dim sqlQuery as String = "SELECT * FROM [main] WHERE [FirstName] IN (" + csvOfSelectedItems + ")"

		' EXECUTE sqlQuery HERE and use the results.
	End If

Open in new window

Avatar of dejandejanovic
dejandejanovic

ASKER

Thank you for replies, but none code is working for me.

I have add a zip file. I hope someone could check it, and fix it.
thank....
Training.zip
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works. Thanks.