我有一个CheckedListBox1
,它的选择创建了一个SQL查询来填充DataGridView。不幸的是,它没有给我任何东西,可能是我的SQL select语句不正确,或者我传递了不正确的字符串值数组
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
Dim i As Integer
'xx is an array of string, used to store checkedlistbox selection
Dim xx() As String
Dim sqlstr2 As String
For i = 0 To CheckedListBox1.Items.Count - 1 Step i + 1
If CheckedListBox1.GetItemCheckState(i) = CheckState.Checked Then
xx(i) = CheckedListBox1.SelectedValue(i)
End If
Next
sqlstr2 = "SELECT * From Table WHERE [TagName] in (' "%, "' '"& [xx] & "' )"'
SQL.RunQuery(sqlstr2)
If SQL.SQLDataset.Tables.Count > 0 Then
DataGridView1.DataSource = SQL.SQLDataset.Tables(0)
End If
End Sub
答案 0 :(得分:0)
在声明另一个数组之前,请检查MS文档。我认为您不希望在每次选中或取消选中一个框时都运行查询。我将代码移到了按钮上,当用户结清支票时可以单击该按钮。
幸运的是,CheckedListBox有一个CheckedItems集合可以循环。在循环中,我们构建了一个字符串以在sql语句的In子句中使用。
检查立即窗口以查看sql语句是否符合您的意图
我只能假设您的其余代码包含您自己的自定义类和方法。我将其注释掉,以便可以测试代码。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim inClause As String = ""
For Each item In CheckedListBox1.CheckedItems
inClause &= "'" & item.ToString & "',"
Next
'Get rid of the final comma
inClause = inClause.TrimEnd(","c)
Dim sqlstr2 = "SELECT * From Table WHERE [TagName] in (" & inClause & ");"
Debug.Print(sqlstr2)
'Sql.RunQuery(sqlstr2)
'If Sql.SQLDataset.Tables.Count > 0 Then
' DataGridView1.DataSource = Sql.SQLDataset.Tables(0)
'End If
End Sub