2011-01-21 105 views
0

我只是写一个填充来自同一数据源3个组合框一个Windows应用程序的奇怪行为。我的数据源是一个数据表。说明需要组合框

我填充组合框的方式是通过重复下面的代码为每个组合框的:

'populate 1st combobox 
cbx1.DataSource = table 
cbx1.DisplayMember = "someColumn" 
cbx1.ValueMember = "anotherColumn" 
cbx1.SelectedIndex = Indx 

'populate 2nd combobox 
cbx2.DataSource = table 
cbx2.DisplayMember = "someColumn" 
cbx2.ValueMember = "anotherColumn" 
cbx2.SelectedIndex = Indx 

'populate 3rd combobox 
cbx3.DataSource = table 
cbx3.DisplayMember = "someColumn" 
cbx3.ValueMember = "anotherColumn" 
cbx3.SelectedIndex = Indx 

当运行应用程序,我从,说,cbx1下拉列表中选择一个项目,我的选择也反映在cbx2和cbx3中。我发现这种行为很奇怪,如果有人能够在幕后解释发生了什么,我会很感激。

在一个侧面说明,我已经能够通过如下所示修改我的代码来解决这个问题,但仍希望有这个看似奇怪的行为作出解释。

'populate 1st combobox 
Dim t1 as datatable = table.Copy 
cbx1.DataSource = t1 
cbx1.DisplayMember = "someColumn" 
cbx1.ValueMember = "anotherColumn" 
cbx1.SelectedIndex = Indx 

'populate 2nd combobox 
Dim t2 as datatable = table.Copy 
cbx2.DataSource = t2 
cbx2.DisplayMember = "someColumn" 
cbx2.ValueMember = "anotherColumn" 
cbx2.SelectedIndex = Indx 

'populate 3rd combobox 
Dim t3 as datatable = table.Copy 
cbx3.DataSource = t3 
cbx3.DisplayMember = "someColumn" 
cbx3.ValueMember = "anotherColumn" 
cbx3.SelectedIndex = Indx 
+0

Windows应用程序? – Novice 2011-01-21 13:17:38

+0

@Jose:是的,这是一个Windows应用程序 – Tracer 2011-01-21 13:18:11

+0

表实例还是一样 – Novice 2011-01-21 13:21:59

回答

1

行为并不奇怪 - 您有三个组合框都绑定到相同的数据源,所以当您在第一个组合框中选择一个值时,您正在更改基础数据源中当前记录的索引 - 因为另外两个组合框是绑定的,它们也会更新。

编辑:在幕后,行为的原因是如何在.Net框架中实现数据绑定 - 有关更详细的解释,请参阅this question

正如你所发现的,该解决方案是使用独立的数据来源为每个组合框。有一个相关的问题here,您可能会感兴趣。

0

这是因为你已经指定DataTable的相同实例的组合框。