Cascading Combo Box setting value back from the Data Table

  • Thread starter Thread starter Shan1986
  • Start date Start date
S

Shan1986

Guest
Hallo,

I have a 3 Cascading combo boxes , i want to store all selected values from combo boxes to data table , this data table is bound to data grid view with binding source. When i select the different rows in DGV i want to set the stored values form the data table to be displayed in the combo boxes as selected values but this part is not working with simple data binding. How do i achieve this?

if you see gif, when select the first row again , child and grand child are set back to first value. That is wrong


Public Class Form1

Dim Storage_BS As New BindingSource

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load



Me.Controls.AddRange(New Control() {cb3, cb2, cb1})
Dim bs1 As New BindingSource With {.DataSource = GetDataSet(), .DataMember = "Tab1"}
cb1.DisplayMember = "Master"
cb1.DataSource = bs1

Dim bs2 As New BindingSource With {.DataSource = bs1, .DataMember = "Rel1"}
cb2.DisplayMember = "Child"
cb2.DataSource = bs2

Dim bs3 As New BindingSource With {.DataSource = bs2, .DataMember = "Rel2"}
cb3.DisplayMember = "GrandChild"
cb3.DataSource = bs3



Dim DataStorage As New DataSet

With DataStorage
With .Tables.Add("Storage")
With .Columns
.Add("Master", GetType(String))
.Add("Child", GetType(String))
.Add("Grand_Child", GetType(String))
End With
End With
End With

Storage_BS.DataSource = DataStorage.Tables("Storage")
DataGridView1.DataSource = Storage_BS



cb1.DataBindings.Add("Text", Storage_BS, "Master", True, DataSourceUpdateMode.OnPropertyChanged)
cb2.DataBindings.Add("Text", Storage_BS, "Child", True, DataSourceUpdateMode.OnPropertyChanged)
cb3.DataBindings.Add("Text", Storage_BS, "Grand_Child", True, DataSourceUpdateMode.OnPropertyChanged)

AddHandler Storage_BS.BindingComplete, AddressOf BindingComplete



End Sub

Private Function GetDataSet() As DataSet
Dim ds As New DataSet
With ds
With .Tables.Add("Tab1")
With .Columns
.Add("ID", GetType(Integer))
.Add("Master", GetType(String))
End With
End With
With .Tables.Add("Tab2")
With .Columns
.Add("ID", GetType(Integer))
.Add("FKMaster", GetType(Integer))
.Add("Child", GetType(String))
End With
End With
With .Tables.Add("Tab3")
With .Columns
.Add("ID", GetType(Integer))
.Add("FKChild", GetType(Integer))
.Add("GrandChild", GetType(String))
End With
End With
For i = 0 To 9
.Tables("Tab1").Rows.Add(i, $"Master {i + 1}")
For k = i * 10 To i * 10 + 9
.Tables("Tab2").Rows.Add(k, i, $"Child {i + 1}-{k + 1}")
For l = k * 10 To k * 10 + 9
.Tables("Tab3").Rows.Add(l, k, $"Grandchild {i + 1}-{k + 1}-{l + 1}")
Next
Next
Next
.Relations.Add("Rel1", .Tables("Tab1").Columns("ID"), .Tables("Tab2").Columns("FKMaster"))
.Relations.Add("Rel2", .Tables("Tab2").Columns("ID"), .Tables("Tab3").Columns("FKChild"))
End With
Return ds
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim Mas As String = cb1.Text
Dim Chi As String = cb2.Text
Dim GrC As String = cb3.Text


Dim nRow = DirectCast(Storage_BS.AddNew(), DataRowView)
nRow("Master") = Mas
nRow("Child") = Chi
nRow("Grand_Child") = GrC

Storage_BS.EndEdit()

End Sub

Sub BindingComplete(ByVal sender As Object, ByVal e As BindingCompleteEventArgs)

If e.BindingCompleteContext = BindingCompleteContext.DataSourceUpdate AndAlso e.Exception Is Nothing Then e.Binding.BindingManagerBase.EndCurrentEdit()

End Sub

End Class


1612727.gif

Thanks

Continue reading...
 
Back
Top