使用CheckedListBox过滤 [英] Filtering using CheckedListBox

查看:121
本文介绍了使用CheckedListBox过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它只能过滤一个已检查的项目。当我检查两个项目时,它只显示我检查的第一个项目。关于逻辑如何的任何想法?



It can only filter one checked item. When I check two items, it only shows the first item that I check. Any idea on how the logic would be?

For item As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
            Dim sql = "select * from Status where Stat= '" & CheckedListBox1.CheckedItems(item) & "'"
            Dim dt As DataTable
            Dim ds As New DataSet
            cnn = New OleDbConnection(connectionString)

            Try
                cnn.Open()
                adptr = New OleDbDataAdapter(sql, cnn)
                adptr.Fill(ds)
                adptr.Dispose()
                cnn.Close()

                dt = ds.Tables(0)
                Form5.DataGridView1.DataSource = dt

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            Form5.DataGridView1.Visible = True
        Next

推荐答案

您只是为各个选中的项目填充DataGridView ...每次循环时,DataTable dt的内容都会发生变化。



最好先建立一个已检查项目列表,然后再使用该列表。单个sql语句例如...

You''re only populating the DataGridView for individual checked items ... the contents of DataTable dt change each time you go around the loop.

You would be better off building up a list of the checked items first and using that list in a single sql statement e.g. ...
Dim statList As String = "("
For item As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
    statList += "'" & CheckedListBox1.CheckedItems(item) & "',"
Next
statList = statList.Substring(0, statList.Length - 1) + ")"

Dim sql = "select * from Status where Stat in " + statList


这篇关于使用CheckedListBox过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆