I have a Winform to display the contents of two related data tables, EquipReq
(the parent) and EqReqItems
(the child).
After filling a new data set (DS
) with both tables, I add a relation to the data set as follows:
DS.Relations.Add("RItems", DS.Tables("EquipReq").Columns("RecID"), DS.Tables("EqReqItems").Columns("ReqNo"), False)
I bind a new binding source BS
as follows:
Dim BS As New BindingSource
BS.DataSource = DS
BS.DataMember = "EquipReq"
I (try) to bind another new binding source BSI
as to the added relation RItems
as follows:
Dim BSI As New BindingSource
BSI.DataSource = DS
BSI.DataMember = "RItems"
At this last line, I get the error "Data member 'RItems' cannot be found in the data source".
I have iterated through the relations of the data set DS
and only one is shown with the name of RItems
. I have also double-checked each data table to ensure the named columns in the relation exist in those tables.
Am I overlooking something obvious? What can I do to resolve this issue?
TIA,
Larry
CodePudding user response:
If you are trying to set up parent/child data-binding then the child binding needs to be to the parent BindingSource
, not the DataSet
. You should add the BindingSources
to the form in the designer, so they are accessible throughout the form code. Also, always set the DataSource
last.
With parentBindingSource
.DataMember = "ChildTable"
.DataSource = myDataSet
End With
With childBindingSource
.DataMember = "ParentChildRelation"
.DataSource = parentBindingSource
End With
The reason you should set DataSource
last, after DataMember
, DisplayMember
and ValueMember
, is that all the binding is set up when you set the DataSource
. If you set it first then all the binding has to be changed when you then set the other property. If you set DataSource
last then all the work of setting up the binding is done only once.