Data Binding Issue

  • Thread starter Thread starter Joseph Hanna
  • Start date Start date
J

Joseph Hanna

I have a table with 2 foreign keys in it that refer back to the same table.


Table 1: TM_CTP_Clocking_Type (Dataset = dsLookup)

CTP_ID Int (PK)
CTP_DESCRIPTION String


Table 2: TM_CLK_Clockings (Dataset = dsEmp)

CLK_ID Int (PK)
CLK_IN_CTP_ID Int (FK to Table 1)
CLK_OUT_CTP_ID Int (FK to Table 1)
CLK_FIELD1 String
etc...

I have a form with several Textboxes that are bound to other fields in Table 2 and they work fine. On the same form, I also have 2 Combo Boxes that are populated with Descriptions from Table 1, and the SelectedValue is set according to the value of the Foreign Keys in Table 2. My problem is at Runtime, both ComboBoxes are set to the same value (that of the second Combo box "cmbCLK_OUT_CTP_ID" - or second Data Binding?).

Here is my Data Binding code:


With Me.cmbCLK_IN_CTP_ID

.DataSource = dsLookup.TM_CTP_Clocking_Type
.ValueMember = "CTP_ID"
.DisplayMember = "CTP_DESCRIPTION"
.SelectedIndex = -1

.DataBindings.Clear()
.DataBindings.Add(New Binding("SelectedValue", dsEmp, "TM_CLK_Clockings.CLK_IN_CTP_ID"))

End With

With Me.cmbCLK_OUT_CTP_ID

.DataSource = dsLookup.TM_CTP_Clocking_Type
.ValueMember = "CTP_ID"
.DisplayMember = "CTP_DESCRIPTION"
.SelectedIndex = -1

.DataBindings.Clear()
.DataBindings.Add(New Binding("SelectedValue", dsEmp, "TM_CLK_Clockings.CLK_OUT_CTP_ID"))

End With

When I comment out the code for the second ComboBox's Data Binding, I get the correct result in the first Combo Box.

Am I doing something wrong in the code or is having 2 relationships to the same table too much for .NET Data Binding?


Many Thanks,
Joe
 
Joseph

You need to create 2 separate BindingContexts so that each combobox works
independently

Dim bc1 As New BindingContext()
Dim bc2 As New BindingContext()
firstCombo.BindingContext = bc1
secondCombo.BindingContext = bc2

Stephen

I have a table with 2 foreign keys in it that refer back to the same table.


Table 1: TM_CTP_Clocking_Type (Dataset = dsLookup)

CTP_ID Int (PK)
CTP_DESCRIPTION String


Table 2: TM_CLK_Clockings (Dataset = dsEmp)

CLK_ID Int (PK)
CLK_IN_CTP_ID Int (FK to Table 1)
CLK_OUT_CTP_ID Int (FK to Table 1)
CLK_FIELD1 String
etc...

I have a form with several Textboxes that are bound to other fields in Table
2 and they work fine. On the same form, I also have 2 Combo Boxes that are
populated with Descriptions from Table 1, and the SelectedValue is set
according to the value of the Foreign Keys in Table 2. My problem is at
Runtime, both ComboBoxes are set to the same value (that of the second Combo
box "cmbCLK_OUT_CTP_ID" - or second Data Binding?).

Here is my Data Binding code:


With Me.cmbCLK_IN_CTP_ID

.DataSource = dsLookup.TM_CTP_Clocking_Type
.ValueMember = "CTP_ID"
.DisplayMember = "CTP_DESCRIPTION"
.SelectedIndex = -1

.DataBindings.Clear()
.DataBindings.Add(New Binding("SelectedValue", dsEmp,
"TM_CLK_Clockings.CLK_IN_CTP_ID"))

End With

With Me.cmbCLK_OUT_CTP_ID

.DataSource = dsLookup.TM_CTP_Clocking_Type
.ValueMember = "CTP_ID"
.DisplayMember = "CTP_DESCRIPTION"
.SelectedIndex = -1

.DataBindings.Clear()
.DataBindings.Add(New Binding("SelectedValue", dsEmp,
"TM_CLK_Clockings.CLK_OUT_CTP_ID"))

End With

When I comment out the code for the second ComboBox's Data Binding, I get
the correct result in the first Combo Box.

Am I doing something wrong in the code or is having 2 relationships to the
same table too much for .NET Data Binding?


Many Thanks,
Joe
 
Back
Top