如何在visual basic 2013中从组合框的选定项中获取关联列的值?

时间:2015-01-22 09:28:04

标签: vb.net visual-studio visual-studio-2013 combobox

我正在使用Visual Basic 2013和sqlite。我的表结构就像 -

  • topic_id
  • TOPIC_NAME
  • subject_id
  • SUBJECT_NAME
  • chapter_no

我将此表导入到组合框中 -

 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导入的,但我不知道如何获取所选项目的相应值。我被困在这里。

1 个答案:

答案 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