database vb.net - 具有 BindingSource 和不同数据源的 ComboBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11434556/
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-08 08:37:33  来源:igfitidea点击:

vb.net - ComboBox with a BindingSource and a different DataSource

databasevb.netdatagridviewcomboboxbindingsource

提问by joharei

In my form I have a DataGridViewbound to a BindingSourcethat has a DataSetas DataSource. I also have some TextFields and ComboBoxes on the form that are bound to different columns in the DataSetthrough the BindingSource. The idea is that the values in the columns on the selected row in the DataGridVieware 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.

在我的表单中,我DataGridView绑定到BindingSource具有DataSetas 的a DataSource。我也有绑定到不同列在表格上的一些文本字段和组合框DataSet通过BindingSource。这个想法是将 中选定行的列中的值DataGridView反映在表单上的其他控件中。也许我说得有点复杂,但连接 TextFields 以及绑定到数据集中表的 ComboBoxes 相当容易。

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

我的问题是,这次我想ComboBox从数组中设置项目,而不是从DataSet. 这是我尝试过的:

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

Now, this seems to work partially as the ComboBoxis 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 ComboBoxColumnthat gets its item list in the way shown above, and it seams to work as expected.

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

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:

如果不清楚;我有几个具有类似功能的组合框,但它们绑定到 a 中的列DataTable,如下所示:

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"

If it matters, the DataSetcomes from an Access Database.

如果重要,则DataSet来自Access Database.

回答by LarsTech

SelectedValueis used in conjunction with the ValueMemberproperty, but since your array list doesn't have descriptive fields, that won't work.

SelectedValueValueMember属性结合使用,但由于您的数组列表没有描述性字段,因此不起作用。

Try using SelectedItemfor your binding:

尝试SelectedItem用于您的绑定:

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

回答by user3692405

I realize this questions is old, but I had similar problem I resolved. I bound the text property of the combo box to the binding source member that relates to the value member or display member of my combo box datasource. Make sure that you fill your data tables (for binding source and combo box datasource) AND bind your combo box to its datasource prior to databinding text of combo box.

我意识到这个问题很旧,但我解决了类似的问题。我将组合框的文本属性绑定到与组合框数据源的值成员或显示成员相关的绑定源成员。确保在组合框的数据绑定文本之前填充数据表(用于绑定源和组合框数据源)并将组合框绑定到其数据源。

Dim dtForBindingSource as DataTable
Dim bs as BindingSource
Dim dtForComboBox as DataTable

'Code to fill dtForBindingSource would go here

bs.DataSource = dtForBindingSource

'Code to fill dtForComboBox would go here

ComboBox.DataSource = dtForComboBox
ComboBox.DisplayMember = "ColumnToDisplay"
ComboBox.ValueMember = "ColumnXYZ"

'Now that datasources exist and combo box is set up I do databindings.

'既然数据源已经存在并且组合框已经设置,我就可以进行数据绑定。

ComboBox.DataBindings.Add("Text", bs, "ColumnToDisplay")