0

我想知道是否可以使用 a 中的多个值过滤表中的数据checkboxlist(or any other way)我有 acheckboxlist和 a gridview,当您选中这些框时,它确实在 gridview 中显示了正确的数据,但是当我尝试检查多个值时会出现问题。它似乎搜索第一个检查值,然后忽略其余的。你会认为这很简单!也许是的。下面是我的尝试。

代码背后

Imports System.Data

Imports System.Data.SqlClient

Partial Class Default2

Inherits System.Web.UI.Page

Dim strSQL As New System.Text.StringBuilder


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack Then
            Dim i As Integer, c As Integer = 0
            Dim strParams As String = ""
            For i = 0 To Me.CheckBoxList1.Items.Count - 1
                If CheckBoxList1.Items(i).Selected Then
                    c += 1
                    If c = 1 Then
                        strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                    Else
                        strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                    End If
                End If
            Next
            strSQL.Append("SELECT Project.*")
            strSQL.Append(" FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID")
            strSQL.Append(" WHERE" & strParams)
            FillGridView()



        End If
    End Sub


    Private Sub FillGridView()

        Dim strMyConn As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FYPMS_DB.mdf;Integrated Security=True"
        Using MyConn As New SqlClient.SqlConnection(strMyConn)
            MyConn.Open()
            Dim cmd As New SqlClient.SqlCommand(strSQL.ToString, MyConn)
            cmd.Connection = MyConn
            cmd.CommandType = CommandType.Text
            Try
                Using dr As SqlClient.SqlDataReader = cmd.ExecuteReader
                    Dim dt As New DataTable
                    dt.Load(dr)


                    Me.GridView1.DataSource = dt
                    Me.GridView1.DataBind()
                End Using
                If Me.GridView1.Visible = False Then Me.GridView1.Visible = True


            Catch ex As Exception
                Me.GridView1.Visible = False
            End Try
        End Using
    End Sub


    Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim i As Integer, c As Integer = 0
        Dim strParams As String = ""
        For i = 0 To Me.CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected Then
                c += 1
                If c = 1 Then
                    strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                Else
                    strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
                End If
            End If
        Next
        If c <> 0 Then
            strSQL.Append("SELECT Project.*")
            strSQL.Append(" FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID")
            strSQL.Append(" WHERE" & strParams)
        End If




    End Sub
End Class
4

1 回答 1

0

重构此部分以创建 WHERE IN 语句,以便检查是否在任何检查的项目中找到该值

Dim strParams As String = ""
For i = 0 To Me.CheckBoxList1.Items.Count - 1
    If CheckBoxList1.Items(i).Selected Then
        c += 1
        If c = 1 Then
            strParams = "(Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
        Else
            strParams &= " AND (Keyword.Keyword = '" & CheckBoxList1.Items(i).Text & "')"
        End If
    End If
Next

Dim params As StringBuilder = New StringBuilder()

For i = 0 To Me.CheckBoxList1.Items.Count - 1
    If CheckBoxList1.Items(i).Selected Then
        params.Append("'") 
        params.Append(CheckBoxList1.Items(i).Text)
        If i < Me.CheckBoxList1.Items.Count Then
            params.Append("',") // don't append a comma if it's the last item
        End If
    End If
Next
strSQL.Append("SELECT Project.* FROM Keyword INNER JOIN Project ON Keyword.ProjID = Project.ProjID WHERE Keyword.Keyword in (")
strSQL.Append(params.ToString()) // append comma delimited values that make up where in statement
strSQL.Append("')") // close where in statement
FillGridView()
于 2015-04-12T22:46:35.483 回答