vb.net - 带有BindingSource和不同DataSource的ComboBox [英] vb.net - ComboBox with a BindingSource and a different DataSource

查看:1619
本文介绍了vb.net - 带有BindingSource和不同DataSource的ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的形式中,我有一个DataGridView绑定到一个BindingSource,它有一个DataSet作为DataSource。我也有一些TextFields和ComboBoxes在窗体上绑定到DataSet中不同的列通过BindingSource。其想法是,DataGridView中所选行上的列中的值反映在窗体上的其他控件中。也许我使它听起来有点复杂,但它很容易连接的TextFields,以及绑定到数据集中的表的组合框。

In my form I have a DataGridView bound to a BindingSource that has a DataSet as DataSource. I also have some TextFields and ComboBoxes on the form that are bound to different columns in the DataSet through the BindingSource. The idea is that the values in the columns on the selected row in the DataGridView are reflected in the other controls on the form. Maybe I make it sound a bit complicated, but it's fairly easy to connect the TextFields, and also the ComboBoxes bound to tables in the dataset.

我的问题是,这个时间我想从数组而不是从DataSet中的表中设置ComboBox中的项目。这是我试过的:

My problem is that this time I want to set the items in the ComboBox from an array and not from a table in the DataSet. This is what I've tried:

Me.ComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue",       Me.TblBindingSource, "ColumnName", True))

Dim ItemArray(2) As String
ItemArray(0) = ""
ItemArray(1) = "Default"
ItemArray(2) = "User-set"
ComboBox.DataSource = ItemArray


$ b b

现在,这似乎是部分工作的ComboBox是正确填充,我可以选择一个值,它出现在DataGridView。但它不会更新其选择的值,因为我更改DataGridView中的行。列(ColumnName)是一个ComboBoxColumn,它以上面显示的方式获取其项目列表,并且其接缝按预期工作。

Now, this seems to work partially as the ComboBox is populated correctly, and I can select a value, and it appears in the DataGridView. But it doesn't update its selected value as I change rows in the DataGridView. The column ("ColumnName") is a ComboBoxColumn that gets its item list in the way shown above, and it seams to work as expected.

如果不清楚;我有几个具有类似功能的ComboBox,可以工作,但是它们绑定到DataTable中的一个列,如下所示:

If it wasn't clear; I have several ComboBoxes with similar functionality that works, but they are bound to a column in a DataTable, as follows:

Me.ComboBox1.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.Tbl1BindingSource, "WiDMethodX", True))
Me.ComboBox1.DataSource = Me.Tbl2BindingSource
Me.ComboBox1.DisplayMember = "SomeColumn"
Me.ComboBox1.ValueMember = "SomeColumn"

事实上,DataSet来自Access数据库。

If it matters, the DataSet comes from an Access Database.

推荐答案

SelectedValue ValueMember 属性结合使用,但由于您的数组列表没有描述性字段,因此无效。

SelectedValue is used in conjunction with the ValueMember property, but since your array list doesn't have descriptive fields, that won't work.

尝试对您的绑定使用 SelectedItem

Me.ComboBox.DataBindings.Add(New Binding("SelectedItem", _
                                      Me.TblBindingSource, "ColumnName", True))

这篇关于vb.net - 带有BindingSource和不同DataSource的ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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