How do I combining sorting in gridview with checkboxes vb

On my webpage.  I have checkboxes to filter the data displayed in the gridview. I works fine with multiple selections. However, I have also enabled sorting on column headers in the gridview using the smart tag in Visual web developer.

When I make a selection with for instance to selections in the checkboxlist and click the button it looks fine. But when I click on a column header to sort the gridview based on the to selections, only data from linked to  one of the two selected values from the checkboxlist will be displayed.

I would apprecitate if some one could give me a hint or maybe even a solution to what is wrong with my code.

I also attach the files here.

Who is Participating?
Ramkisan JagtapConnect With a Mentor Lead DeveloperCommented:
Please debug your code. I think when you are clicking on row header for sorting it is taking only first selected checkbox as a parameter. try with adding your below code on page load event too.

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

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

            ' Prepare Query string
            Dim sqlQuery As String = "SELECT * FROM dbo_statistik WHERE [kommunnamn] IN (" + csvOfSelectedItems + ")"

            ' EXECUTE sqlQuery HERE and use the results.
            SqlDataSource1.SelectCommand = sqlQuery
        End If

marcguAuthor Commented:
Thanks a lot! It worked perfectly! Sorry for not checking that first!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.