问题描述
如何根据CheckedListBox中的多个选中项从数据库中检索数据?请指导我!!!
我目前正在做一个关于根据 CheckedListBox 中的多个选中项从表中检索数据的教程.现在我只能通过 1 个选中的项目从表中检索数据.如何使其可被多个选中的项目检索?
I'm currently doing a tutorial regarding to retrieve data from table according to multiple checked items from a CheckedListBox. Right now i'm able to retrieve data from table by only 1 checked item. how to make it retrievable by multiples checked item?
- checklistbox1
- checklistbox2
本教程将是:首先将所有相关数据作为项目加载到 checklistbox1 中,用户可以去检查列出的项目,一旦用户检查了详细信息,checklistbox2 现在将从另一个表中查询数据,其中[field] = checklistbox1 中选中的项目.
The tutorial will be: first load all related data into checklistbox1 as item, and user(s) may go and check the listed item, once the user checked particulars item(s) checklistbox2 will now query out data from another table where [field] = checked item(s) from checklistbox1.
将事件作为类加载
Public Sub Startload()
Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=False;")
Dim dt1 As New DataTable
Dim sqlstr As String = "SELECT * FROM tbl"
Dim command As New OleDbCommand(sqlstr, connection)
Dim adpt As New OleDbDataAdapter(command)
adpt.SelectCommand = command
adpt.Fill(dt1)
CheckedListBox1.DisplayMember = "name"
CheckedListBox1.ValueMember = "ID"
CheckedListBox1.DataSource = dt1
End Sub
当 Checked 更改时执行 checkedload()
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
Label1.Text = CheckedListBox1.SelectedValue
checkedload()
End Sub
checklistbox1(checkedload)中选中的项目后将执行并检索数据并显示在checklistbox2中
Private Sub checkedload()
Dim x As String = Label1.Text
Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=False;")
Dim dt2 As New DataTable
Dim sqlstr2 As String = "SELECT * FROM tbl2 WHERE [name]='" & x & "'"
Dim command2 As New OleDbCommand(sqlstr2, connection)
Dim adpt2 As New OleDbDataAdapter(command2)
adpt2.SelectCommand = command2
adpt2.Fill(dt2)
CheckedListBox2.DisplayMember = "namex"
CheckedListBox2.ValueMember = "ID"
CheckedListBox2.DataSource = dt2
End Sub
推荐答案
自行解决
Dim i As Integer
Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=False;")
Dim dt2 As New DataTable
For i = 0 To CheckedListBox1.Items.Count - 1 Step i + 1
If CheckedListBox1.GetItemCheckState(i) = CheckState.Checked Then
Dim xx As String = (CType(CheckedListBox1.Items(i), DataRowView))("name")
Dim sqlstr2 As String = "SELECT * FROM tbl2 WHERE [name]='" & xx & "'"
Dim command2 As New OleDbCommand(sqlstr2, connection)
Dim adpt2 As New OleDbDataAdapter(command2)
adpt2.SelectCommand = command2
adpt2.Fill(dt2)
CheckedListBox2.DisplayMember = "namex"
CheckedListBox2.ValueMember = "ID"
CheckedListBox2.DataSource = dt2
End If
Next
这篇关于根据 CheckedListBox 中的多个选中项从数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!