vb.net 将组合框绑定到数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18541474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:52:57  来源:igfitidea点击:

vb.net Bind a combobox to a datasource

vb.netbindingcombobox

提问by Corbin de Bruin

Over the past few days, I've read about a thousand forum posts, and looked at many code samples, but I still can't figure this out.

这几天,我读了大约一千个论坛帖子,看了很多代码示例,但我仍然无法弄清楚这一点。

My goal is to populate a combobox with values from an Access 2007 query. I can do this by using the DataSource, DisplayMember and ValueMember properties, but regardless of the data in the query, the comboboxes all just default to the to first item in the items collection and don't change when the main BindingSource is moved. I am binding the controls with this line of code:

我的目标是使用 Access 2007 查询中的值填充组合框。我可以通过使用 DataSource、DisplayMember 和 ValueMember 属性来做到这一点,但不管查询中的数据如何,组合框都只是默认为项目集合中的第一个项目,并且在移动主 BindingSource 时不会更改。我用这行代码绑定控件:

 ComboBox1.DataSource = DataSet1.qryItemSourceTest
 ComboBox1.DisplayMember = "SourceTestDisplayField"
 ComboBox1.ValueMember = "SourceTestIDField"
 Combobox1.DataBindings.Add(New Binding("SelectedValue", qryTestBindingSource, "TestField", True))

I have also tried using a BindingSource as a DataSource. I have also tried using an array as a DataSource.

我也尝试过使用 BindingSource 作为数据源。我也试过使用数组作为数据源。

If I drag the control on from the Data Source tab in the IDE, it will scroll through records properly, but it will only display the value and not the query 'look-up' value that I want the combobox to display. I have tried changing the DisplayMember and ValueMember properties of the combobox after dragging it on from the Data Sources tab and that seems to break the functionality as well.

如果我从 IDE 的“数据源”选项卡上拖动控件,它将正确滚动记录,但它只会显示值,而不是我希望组合框显示的查询“查找”值。从“数据源”选项卡中拖动组合框后,我尝试更改它的 DisplayMember 和 ValueMember 属性,这似乎也破坏了功能。

I'm looking for some best practices here. I know I'm missing something easy here and I apologize for post an issue that has already been covered so many times, but I could use some individual help here.

我在这里寻找一些最佳实践。我知道我在这里遗漏了一些简单的东西,我很抱歉发布一个已经被多次提及的问题,但我可以在这里使用一些个人帮助。

Edit:I eventually was able to accomplish my goal using the Visual Studio IDE Properties window. The second property on a control is a Data Bindings property which expands into exactly what I needed. I just never saw this before because it was not in alphabetically order like everything else.

编辑:我最终能够使用 Visual Studio IDE 属性窗口实现我的目标。控件上的第二个属性是 Data Bindings 属性,它扩展为我所需要的。我以前从未见过这个,因为它不像其他所有东西那样按字母顺序排列。

采纳答案by SMHasnain

Hi you can do this code to populate data into combo

嗨,您可以执行此代码将数据填充到组合中

Try

        Dim cn As New OleDbConnection
        cn.ConnectionString = conString
        cn.Open()

        cmd.Connection = cn
        cmd.CommandText = "your query"

        'Execte reader function is used to hold more than one value from the table
        dr = cmd.ExecuteReader()


        ' Fill a combo box with the datareader
        Do While dr.Read = True
            ComboBox1 = dr.Item(0)
        Loop

        cn.Close()

    Catch ex As Exception
        MsgBox(ex.Message)

    End Try

If you have any more problems don't hesitate to ask.

如果您有更多问题,请随时提出。