ADO Relate 2 tables by more then one field






1.60/5 (9 votes)
Apr 7, 2004

30652
Relate 2 tables by more then one field
Introduction
The way to stablish a System.Data.DataRelation betwin 2 System.Data.DataTable's by 2 or more System.Data.datacolumn's.
In this example u must asume the existence of 2 DataTables in the DS_Geral DataSet with the acording fields ...
Sub Ex()
Dim DR_Afectacao_Producao as System.Data.DataRelation
Dim Parent (1) as System.Data.datacolumn
Dim Child (1) as System.Data.datacolumn
'This arrays of datacolumns must be delimited by the number of fields
'conected in the relation,
'thei are 0-based
Parent (0) = DT_Afectacao.Columns("id_prod") 'The parent Table
Parent (1) = DT_Afectacao.Columns("id_xnuc") 'The parent Table
Child (0) = DT_Producao.Columns("id_prod") 'The child Table
Child (1) = DT_Producao.Columns("id_xnuc") 'The child Table
DR_Afectacao_Producao = new Datarelation ("Afect_Producao", Parent, Child, _
false )
DS_Geral.Relations.Add(DR_Afectacao_Producao)
End Sub
Example 1 Geting the Child Rows:
Dim ChildRows_1(), foundRow As DataRow
foundRow = DT_Afectacao.Rows.Find(Sender.Selecteditem.value)
ChildRows_1 = foundRow.GetChildRows("Afect_Producao")
Result: ChildRows_1 is an Array of all of the child Rows ...
Example 2 Geting the Parent row:
Dim founded_ParentRow , foundRow As DataRow
foundRow = DT_Producao.Rows.Find(Sender.Selecteditem.value)
founded_ParentRow = foundRow.GetParentRow("Afect_Producao")
Result: foundParentRow is a DataRow in the parent DataTable of the relation ...
Summary:
The "Rows.Find" only finds key-values ... in the key-column of the DataTable