我正在使用Visual Basic 2013和sqlite。我的表结构就像 -
我将此表导入到组合框中 -
Dim con As SQLiteConnection
Dim sql As String
Dim da As SQLiteDataAdapter
Dim ds As New DataSet
Dim cmd As New SQLiteCommand
con = New SQLiteConnection("Data Source = test.db;Version=3;")
con.Open()
sql = "SELECT * FROM med_topics"
da = New SQLiteDataAdapter(sql, con)
da.Fill(ds, "TopicsList")
con.close()
cboTopicsList.DisplayMember = "Topic_Name"
cboTopicsList.ValueMember = "Topic_Id"
cboTopicsList.DataSource = ds.Tables("TopicsList")
我想使用其他列值的表(但不要让它们显示在组合框中)即。 " subject_id,subject_name,chapter_no"。
我知道这些值是TopicsList
导入的,但我不知道如何获取所选项目的相应值。我被困在这里。
答案 0 :(得分:0)
ComboBox
在绑定到DataTable
时,使用DataRowView个对象填充其对象集合( Items 属性)。这是因为DataTable
类实现了ComboBox
寻找的IListSource。它会返回DataView,表格的DefaultView。
Private Sub cboTopicsList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboTopicsList.SelectedIndexChanged
Dim rowView As DataRowView = TryCast(cboTopicsList.SelectedItem, DataRowView)
If (Not rowView Is Nothing) Then
Dim row As DataRow = rowView.Row
Dim subjectName As String = DirectCast(row.Item("subject_name"), String)
'...
End If
End Sub