这是在以前解析的与组合框中的Access数据库值相关的查询的延续。
下面的代码工作正常。即,它将Access表列中的数据导入到组合框的下拉列表中。我想要的是如何根据Combobox.SelectedIndex分配这些值?我添加了TagComboBox1.SelectedIndex = 0
,但我想要一些会自动为索引到组合框下拉列表中填充的值的内容。
源表是tag_data'
键列是tag_unique_id
我需要它,因为我想使用combobox.selectedindexchanged事件来执行更多任务。我玩了但没有成功。
Dim dt As New DataTable
Dim query As String = "select tag_unique_id from tag_data order by tag_unique_id"
Using connection As New OleDbConnection(strConnectionString)
Using command As New OleDbCommand(query, connection)
Using adapter As New OleDbDataAdapter(command)
connection.Open()
adapter.Fill(dt)
connection.Close()
End Using
End Using
End Using
If dt.Rows.Count > 0 Then
TagComboBox1.DataSource = dt
TagComboBox1.ValueMember = "tag_unique_id"
TagComboBox1.DisplayMember = "tag_unique_id"
TagComboBox1.SelectedIndex = 0
End If
更新:以下是combobox.selectedindexchanged代码。在项目工作期间,索引可以是任何内容,因此我可以使用一个变量来为selectedindexchanged事件指定新值,而不是指定为selectedindex=0 (1,2,3..)
吗?
Private Sub TagComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As system.EventArgs) Handles TagComboBox1.SelectedIndexChanged
'reference http://stackoverflow.com/questions/13152534/view-data-from-database-using-textbox
Dim dt As New DataTable
Dim query As String = "select tag_text from tag_data"
Dim dr As OleDbDataReader
Dim connection As New OleDbConnection(strConnectionString)
Dim command As New OleDbCommand(query, connection)
connection.Open()
If TagComboBox1.SelectedIndex = 0 Then
dr = command.ExecuteReader
While dr.Read()
TagTextBox.Text = dr("tag_text").ToString
End While
dr.Close()
End If
答案 0 :(得分:-1)
如果您尝试根据组合框的选定索引填充文本框,则需要执行此操作。
For index As Integer = 0 To (ComboBox1.Items.Count - 1)
textbox.Text = ComboBox1.Items(index)
Next