2015-01-07 41 views
1

我準備了一個小項目來演示我的問題。它由兩個具有父子關係的數據表組成。我用框架4.0使用vb.net 2010。
- 開始一個新的WinForm項目。
- 在Form1設計器上,拖動一個BindingNavigator,一個Button和兩個ComboBox。
- 在後面的代碼,複製代碼如下:在綁定組合框中設置空值時出現奇怪的行爲

Public Class Form1 
Private dsMain As DataSet 
Private WithEvents bndSource As New BindingSource 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    dsMain = New DataSet 

    'Create tables 
    Dim dtContacts As New DataTable("Contacts") 
    Dim col As DataColumn = dtContacts.Columns.Add("IDContact", GetType(Integer)) 
    col.AllowDBNull = False 
    col.AutoIncrement = True 
    col.Unique = True 
    dtContacts.Columns.Add("FullName") 
    col = dtContacts.Columns.Add("IDCountry", GetType(Integer)) 
    col.AllowDBNull = True 
    dsMain.Tables.Add(dtContacts) 

    Dim dtCountries As New DataTable("Countries") 
    col = dtCountries.Columns.Add("IDCountry", GetType(Integer)) 
    col.AllowDBNull = False 
    col.AutoIncrement = True 
    col.Unique = True 
    dtCountries.Columns.Add("A2ISO") 
    dtCountries.Columns.Add("CountryName") 
    dsMain.Tables.Add(dtCountries) 

    'Add relation 
    Dim rel As New DataRelation("rel", dtCountries.Columns("IDCountry"), dtContacts.Columns("IDCountry")) 
    dsMain.Relations.Add(rel) 

    'Populate parent table 
    dtCountries.Rows.Add(1, "AF", "Afghanistan") 
    dtCountries.Rows.Add(2, "AL", "Albania") 
    dtCountries.Rows.Add(3, "DZ", "Algeria") 

    'Populate child table 
    dtContacts.Rows.Add(1, "First Contact", 3) 
    dtContacts.Rows.Add(2, "Second Contact", 1) 

    'Set bindings 
    bndSource.DataSource = dtContacts.DefaultView 
    BindingNavigator1.BindingSource = bndSource 

    ComboBox1.DataSource = dtCountries 
    ComboBox1.DisplayMember = "A2ISO" 
    ComboBox1.ValueMember = "IDCountry" 
    ComboBox1.DataBindings.Add("SelectedValue", bndSource, "IDCountry") 
    ComboBox2.DataSource = dtCountries 
    ComboBox2.DisplayMember = "CountryName" 
    ComboBox2.ValueMember = "IDCountry" 
    ComboBox2.DataBindings.Add("SelectedValue", bndSource, "IDCountry") 

    dsMain.AcceptChanges() 
End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    ComboBox1.Text = Nothing 'Set IDCountry to DBNull, same as ComboBox1.SelectedIndex = -1 
    ComboBox2.Text = Nothing 
End Sub 
End Class 

- 運行項目。
- 嘗試前後導航,並更改組合框中的項目:全部都是正確的。
- 現在在聯繫人表中設置IDCountry = Null,因爲它是允許的。要實現這一點,請點擊Button1。
- 在ComboBox1中,如果選擇與單擊按鈕之前相同的項目,則會看到行爲不再正確:ComboBox2不會使用ComboBox1相應更新,而是保持爲空。
- 如果您選擇另一個項目,從現在開始,所有項目都是正確的。
這是.net數據綁定中的錯誤嗎?如果是這樣,是否有任何解決方法?
在此先感謝。

回答

0

這是我找到的解決方法。我沒有找到我觀察到的行爲的原因,但此代碼適用於我:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged 
If ComboBox1.SelectedIndex < ComboBox2.Items.Count Then ComboBox2.SelectedIndex = ComboBox1.SelectedIndex 
End Sub 

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged 
If ComboBox2.SelectedIndex < ComboBox1.Items.Count Then ComboBox1.SelectedIndex = ComboBox2.SelectedIndex 
End Sub 

它簡單地使ComboBox選擇的索引相等。
反正謝謝你。

相關問題