vb.net绑定组合框到数据源 [英] vb.net Bind a combobox to a datasource
问题描述
在过去的几天里,我读过约一千论坛帖子,看着许多code样品,但我还是不明白这一点。
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.
我的目标是从2007年Access查询填充值的组合框。我可以通过使用数据源,和的DisplayMember属性ValueMember做到这一点,但是无论在查询数据时,组合框都只是默认为在项目收集第一项,当主的BindingSource移动不会改变。
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:
ComboBox1.DataSource = DataSet1.qryItemSourceTest
ComboBox1.DisplayMember = "SourceTestDisplayField"
ComboBox1.ValueMember = "SourceTestIDField"
Combobox1.DataBindings.Add(New Binding("SelectedValue", qryTestBindingSource, "TestField", True))
我也使用的BindingSource作为一个DataSource尝试。
我一直在使用一个数组作为一个DataSource也试过。
I have also tried using a BindingSource as a DataSource. I have also tried using an array as a DataSource.
如果我从IDE数据源选项卡拖动控制,它将滚动浏览记录正常,但它只会显示该值,而不是查询'查询'的价值,我想在组合框中显示。我试图从数据源标签拖动后更改组合框的的DisplayMember和ValueMember属性和似乎打破了功能,以及
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.
我在寻找这里的一些最佳做法。我知道我失去了一些东西很容易在这里和我的职位,已经涵盖了很多次的问题道歉,但我可以在这里使用了一些个人的帮助。
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.
编辑:我最终能够完成使用Visual Studio IDE属性窗口中我的目标。在控制第二个属性是扩展到正是我需要的数据绑定属性。我从来没见过这个,因为它按字母顺序为了和其他事物一样没有。
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.
推荐答案
你可以这样做code数据填充到组合
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.
这篇关于vb.net绑定组合框到数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!