2011-01-21 104 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的相同實例的組合框。