System.Data Namespace DataTable Class
Returns the collection of child relations for this DataTable.
Script |
[ DataRelationCollection variable = ] DataTable.ChildRelations |
The property is read only with no default value.
A DataRelation defines the relationship between two tables. Typically, two tables are linked through a single field that contains the same data. For example, a table which contains address data may have a single field containing codes that represent countries/regions. A second table that contains country/region data will have a single field that contains the code that identifies the country/region, and it is this code which is inserted into the corresponding field in the first table.
A DataRelation, then, contains at least four pieces of information:
- the name of the first table,
- the column name in the first table,
- the name of the second table, and
- the column name in the second table.
The following example uses the ChildRelations property to return each child DataRelation in a DataTable. Each relation is then used as an argument in the GetChildRows method of the DataRow to return an array of rows. The value of each column in the row is then printed.
Private void ChildRelationsDemo ( DataTable myTable ) {
/* For each row in the table, get the child rows using the ChildRelations.
For each item in the array, print the value of each column. */
DataRow [ ] arrRows;
foreach ( DataRelation myRelation in myTable.ChildRelations ) {
foreach ( DataRow myRow in myTable.Rows ) {
arrRows = myRow.GetChildRows ( myRelation );
// print values of rows.
for ( int i = 0; i < arrRows.Length; i++ ) {
foreach ( DataColumn myColumn in myTable.Columns ) {
Response.Write ( arrRows [ ] [ myColumn.ColumnName ] );
}
}
}
}
}
Private Sub ChildRelationsDemo ( ByVal myTable As DataTable )
' For each row in the table, get the child rows using ChildRelations.
' For each item in the array, print the value of each column.
Dim arrRows ( ) As DataRow, myRelation As DataRelation
Dim myRow As DataRow, myColumn As DataColumn, i As Integer
For Each myRelation in myTable.ChildRelations
For Each myRow In myTable.Rows
arrRows = myRow.GetChildRows ( myRelation )
' print values of rows.
For i = 0 to arrRows.GetUpperBound ( 0 )
For Each myColumn In myTable.Columns
Response.Write ( arrRows ( i ) ( myColumn.ColumnName ) )
Next
Next
Next
Next
End Sub |
|
C# |
VB |
DataTable Members ParentRelations DataRelation GetParentRows GetChildRows