简体   繁体   English

如何从Visual Basic 2013中的组合框的选定项中获取关联列的值?

[英]How to get associated column's value from selected item of combo-box in visual basic 2013?

I am working with Visual Basic 2013 and sqlite. 我正在使用Visual Basic 2013和sqlite。 My table structure is like - 我的表结构就像-

  • topic_id topic_id
  • topic_name TOPIC_NAME
  • subject_id subject_id
  • subject_name SUBJECT_NAME
  • chapter_no chapter_no

I am importing this table into combo box as - 我将此表导入为-

 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")

I want to use tables other column's value (but don't want them to display in combo box) viz. 我想使用其他列值的表(但不希望它们显示在组合框中)。 "subject_id, subject_name, chapter_no". “主题ID,主题名称,章节编号”。 I know these values are imported in TopicsList , but I don't know, how to get the corresponding value of the selected item. 我知道这些值是在TopicsList中导入的,但是我不知道如何获取所选项目的相应值。 I am stuck here. 我被困在这里。

A ComboBox populate its object collection (the Items property) with DataRowView objects when bound to a DataTable . ComboBox绑定到DataTable时,使用DataRowView对象填充其对象集合( Items属性)。 This is because the DataTable class implements IListSource which the ComboBox looks for. 这是因为DataTable类实现了ComboBox查找的IListSource It returns a DataView , the DefaultView of the table. 它返回一个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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM