如何循环通过MS Access表列值拉在组合框(VB .Net) [英] How to loop through MS Access table-column values pulled in a Combo box (VB .Net)

查看:197
本文介绍了如何循环通过MS Access表列值拉在组合框(VB .Net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是与组合框中的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
使用连接作为新的OleDbConnection(strConnectionString)
使用命令作为新的OleDbCommand(查询,连接)
使用适配器作为新的OleDbDataAdapter(命令)
connection.Open()
适配器。 Fill(dt)
connection.Close()
结束使用
结束使用
结束使用
如果dt.Rows.Count> 0 Then
TagComboBox1.DataSource = dt
TagComboBox1.ValueMember =tag_unique_id
TagComboBox1.DisplayMember =tag_unique_id
TagComboBox1.SelectedIndex = 0
结束如果

UPDATE:下面是combobox.selectedindexchanged代码。索引可以是在项目工作期间的任何东西,因此,而不是指定 selectedindex = 0(1,2,3 ..)可以有一个变量,将分配一个新的value to selectedindexchanged事件?

  Private Sub TagComboBox1_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As system.EventArgs)Handles TagComboBox1。 SelectedIndexChanged 
'参考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连接作为新的OleDbConnection(strConnectionString)
Dim命令作为新的OleDbCommand(查询,连接)
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


解决方案

如果您要根据所选的组合框索引来填充文本框,这就是您需要做的。

 对于索引As Integer = 0 To(ComboBox1.Items.Count  -  1)
textbox.Text = ComboBox1.Items(index)
Next


This is in continuation of a previously resolved query related to Access Database values in Combo box. The code below is working fine. i.e. it gets the data from an Access table column into the dropdown of a combo-box. What I want is how to assign those values against the Combobox.SelectedIndex? I added TagComboBox1.SelectedIndex = 0 but I want something that will automatically assign Index to the values populated in the combo-box dropdown. The source table is tag_data ' Key Column is tag_unique_id

I need it as I want to use the combobox.selectedindexchanged event to carry out further tasks. I played around but did not succeed.

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

UPDATE: Below is the combobox.selectedindexchanged code. The index can be anything during working of project and hence instead of specifying as selectedindex=0 (1,2,3..) can I have a variable which will assign a new value to selectedindexchanged event?

 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

解决方案

If you are trying to populate a textbox according to the selected index of the combobox this is what you need to do.

For index As Integer = 0 To (ComboBox1.Items.Count - 1)
        textbox.Text = ComboBox1.Items(index)
    Next

这篇关于如何循环通过MS Access表列值拉在组合框(VB .Net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆