2017-08-25 216 views
0

我想使用Windows窗體顯示從項目有關兒童的數據單項和信息(=父 - 蔡爾茲)創造的東西NET WinForms控件數據綁定親子

事情是這樣的: Parent - Child Relation

--For exemple, i have 
--MyParent with properties 
MyParent.Data1 
MyParent.Data2 
MyParent.Data3 
MyParent.Childs() 

--And for child 
MyChild.Data1 
MyChild.Data2 
MyChild.Data3 

所以,在第一個Combobox中,我有我的項目清單, 在第二個,我有我的孩子從當前選定的父項目

我沒有任何問題的綁定父,但這是另一個孩子的歷史!

Public bs As BindingSource = New BindingSource() 
[...] 
bs.DataSource = New BindingList(Of FopEtatFichier)(DateMyData)) 
TxtData1.DataBindings.Add("Text", bs, "Data1", True, DataSourceUpdateMode.OnPropertyChanged) 
TxtData2.DataBindings.Add("Text", bs, "Data2", True, DataSourceUpdateMode.OnPropertyChanged) 
TxtData3.DataBindings.Add("Text", bs, "Data3", True, DataSourceUpdateMode.OnPropertyChanged) 
ChkEtatSupprime.DataBindings.Add("Checked", bs, "EstSupprime", True, DataSourceUpdateMode.OnPropertyChanged) 

我是否有義務爲孩子創建另一個綁定源? 我做了十分可怕的事情:

Private Sub CbbChild_SelectedValueChanged(sender As Object, e As EventArgs) Handles CbbChild.SelectedValueChanged, CbbChild.DataSourceChanged 

ChildData1.DataBindings.Clear() 
ChildData2.DataBindings.Clear() 
ChildData3.DataBindings.Clear() 

ChildData1.DataBindings.Add("Text", CbbChild.SelectedItem, "ChildData1", True, DataSourceUpdateMode.OnPropertyChanged) 
ChildData2.DataBindings.Add("Checked", CbbChild.SelectedItem, "ChildData2", True, DataSourceUpdateMode.OnPropertyChanged) 
ChildData3.DataBindings.Add("Text", CbbChild.SelectedItem, "ChildData3", True, DataSourceUpdateMode.OnPropertyChanged) 

是否有可能使用baove事情不總是清除/添加?

(你可以提供C#代碼,如果你prefere,不管)

感謝您的幫助!

+0

當選擇更改時,不會更改綁定。整個綁定點是你設置一次,然後自動完成。檢查了這一點:http://www.vbforums.com/showthread.php?518065-Master-Detail-(Parent-Child)-Data-binding-(-NET-2-0-WinForms) – jmcilhinney

+0

是的,這就是問題所在,正如我所說,«非常醜陋»:( – Testman

+0

嗯......我剛剛意識到,正在使用一個'BindingList'作爲基礎數據源。是否需要?我的建議可以與DataSet一起使用,因爲它包含'DataRelations '。另一個數據源,你需要做更多的工作 – jmcilhinney

回答

1

這個例子爲我工作。創建一個WinForms應用程序項目並將parentComboBox,parentTextBox,parentBindingSource,childComboBox,childTextBoxchildBindingSource添加到窗體中。

Public Class Form1 

    Private parents As New List(Of Parent) From {New Parent With {.ParentId = 1, .Name = "Parent 1"}, 
               New Parent With {.ParentId = 2, .Name = "Parent 2"}, 
               New Parent With {.ParentId = 3, .Name = "Parent 3"}} 
    Private children As New List(Of Child) From {New Child With {.ChildId = 1, .ParentId = 1, .Name = "Child 1"}, 
               New Child With {.ChildId = 2, .ParentId = 2, .Name = "Child 2"}, 
               New Child With {.ChildId = 3, .ParentId = 3, .Name = "Child 3"}, 
               New Child With {.ChildId = 4, .ParentId = 1, .Name = "Child 4"}, 
               New Child With {.ChildId = 5, .ParentId = 2, .Name = "Child 5"}, 
               New Child With {.ChildId = 6, .ParentId = 3, .Name = "Child 6"}, 
               New Child With {.ChildId = 7, .ParentId = 1, .Name = "Child 7"}, 
               New Child With {.ChildId = 8, .ParentId = 2, .Name = "Child 8"}, 
               New Child With {.ChildId = 9, .ParentId = 3, .Name = "Child 9"}} 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     'Use a dummy list to enable configuring the bindings without any data. 
     childBindingSource.DataSource = New List(Of Child) 

     With childComboBox 
      .DisplayMember = "ChildId" 
      .ValueMember = "ChildId" 
      .DataSource = childBindingSource 
     End With 

     childTextBox.DataBindings.Add("Text", childBindingSource, "Name") 

     parentBindingSource.DataSource = parents 

     With parentComboBox 
      .DisplayMember = "ParentId" 
      .ValueMember = "ParentId" 
      .DataSource = parentBindingSource 
     End With 

     parentTextBox.DataBindings.Add("Text", parentBindingSource, "Name") 
    End Sub 

    Private Sub parentComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles parentComboBox.SelectedIndexChanged 
     Dim parentId = CInt(parentComboBox.SelectedValue) 

     'With this line, the first child will be selected after selecting a new parent. 
     'Without this line, the child SelectedIndex will remain unchanged even though the SelectedItem has changed. 
     childBindingSource.DataSource = New List(Of Child) 

     childBindingSource.DataSource = children.Where(Function(child) child.ParentId = parentId) 
    End Sub 

End Class 


Public Class Parent 

    Public Property ParentId As Integer 

    Public Property Name As String 

End Class 


Public Class Child 

    Public Property ChildId As Integer 

    Public Property ParentId As Integer 

    Public Property Name As String 

End Class 
+0

這樣做的竅門!謝謝! – Testman