Export (0) Print
Expand All

DataTable.ChildRelations Property

Gets the collection of child relations for this DataTable.

[Visual Basic]
Public ReadOnly Property ChildRelations As DataRelationCollection
[C#]
public DataRelationCollection ChildRelations {get;}
[C++]
public: __property DataRelationCollection* get_ChildRelations();
[JScript]
public function get ChildRelations() : DataRelationCollection;

Property Value

A DataRelationCollection that contains the child relations for the table; otherwise a null value if no DataRelation objects exist.

Remarks

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: (1) the name of the first table, (2) the column name in the first table, (3) the name of the second table, and (4) the column name in the second table.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Sub GetChildRowsFromDataRelation()
    ' 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.
    Dim myTable As DataTable
    myTable = CreateDataSet().Tables( "Customers" )

    Dim childRows() As DataRow
    Dim myRelation as DataRelation
    Dim myRow as DataRow
    For Each  myRelation In myTable.ChildRelations
        For Each myRow In myTable.Rows
            PrintRowValues( new DataRow() {myRow}, "Parent Row" )
            childRows = myRow.GetChildRows(myRelation)
            ' Print values of rows.
            PrintRowValues( childRows, "child rows" )
        Next myRow
    Next myRelation
End Sub

Public Function CreateDataSet() As DataSet
    ' create a DataSet with one table, two columns
    Dim myDataSet As DataSet
    myDataSet = new DataSet()

    ' create Customer table
    Dim t As DataTable
    t = new DataTable( "Customers" )

    myDataSet.Tables.Add( t )
    t.Columns.Add( "customerId", GetType(Integer) ).AutoIncrement = true
    t.Columns.Add( "name", GetType(String) )
    t.PrimaryKey = new DataColumn() { t.Columns("customerId") }

    ' create Orders table
    t = new DataTable( "Orders" )
    myDataSet.Tables.Add( t )
    t.Columns.Add( "orderId", GetType(Integer) ).AutoIncrement = true
    t.Columns.Add( "customerId", GetType(Integer) )
    t.Columns.Add( "amount", GetType(Double) )
    t.PrimaryKey = new DataColumn() { t.Columns("orderId") }

    ' create relation
    myDataSet.Relations.Add( myDataSet.Tables("Customers").Columns("customerId"), _
        myDataSet.Tables("Orders").Columns("customerId") )
    
    ' populate the tables
    Dim orderId As Integer = 1
    Dim customerId As Integer
    Dim i As Integer
    For customerId = 1 To 10
        ' add customer record
        myDataSet.Tables("Customers").Rows.Add( _
            new object() { customerId, string.Format("customer{0}", customerId) } )
        
        ' add 5 order records for each customer

        For i = 1 To 5
            myDataSet.Tables("Orders").Rows.Add( _
                new object() { orderId, customerId, orderId * 10 } )
        
        orderId = orderId+1 
    Next
    Next

    CreateDataSet = myDataSet
End Function

private sub PrintRowValues( rows() As DataRow, label As String )
    Console.WriteLine( "\n{0}", label )
    If rows.Length <= 0
        Console.WriteLine( "no rows found" )
        Exit Sub
    End If

    Dim r As DataRow
    Dim c As DataColumn

    For Each r In rows
        For Each c In r.Table.Columns
            Console.Write( "\t {0}", r(c) )
        Next c
        Console.WriteLine()
    Next r
End Sub

[C#] 
private static void GetChildRowsFromDataRelation()
{
    /* 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. */
    DataTable myTable = CreateDataSet().Tables[ "Customers" ];
    DataRow[] childRows;
    foreach(DataRelation myRelation in myTable.ChildRelations)
    {
        foreach(DataRow myRow in myTable.Rows)
        {
            PrintRowValues( new DataRow[] {myRow}, "Parent Row" );
            childRows = myRow.GetChildRows(myRelation);
            // Print values of rows.
            PrintRowValues( childRows, "child rows" );
        }
    }
}

public static DataSet CreateDataSet()
{
    // create a DataSet with one table, two columns
    DataSet myDataSet = new DataSet();

    // create Customer table
    DataTable t = new DataTable( "Customers" );
    myDataSet.Tables.Add( t );
    t.Columns.Add( "customerId", typeof(int) ).AutoIncrement = true;
    t.Columns.Add( "name", typeof(string) );
    t.PrimaryKey = new DataColumn[] { t.Columns["customerId"] };

    // create Orders table
    t = new DataTable( "Orders" );
    myDataSet.Tables.Add( t );
    t.Columns.Add( "orderId", typeof(int) ).AutoIncrement = true;
    t.Columns.Add( "customerId", typeof(int) );
    t.Columns.Add( "amount", typeof(double) );
    t.PrimaryKey = new DataColumn[] { t.Columns["orderId"] };

    // create relation
    myDataSet.Relations.Add( myDataSet.Tables[ "Customers" ].Columns[ "customerId" ],
        myDataSet.Tables[ "Orders" ].Columns[ "customerId" ] );
    
    // populate the tables
    int orderId = 1;
    for( int customerId=1; customerId<=10; customerId++ )
    {
        // add customer record
        myDataSet.Tables["Customers"].Rows.Add( 
            new object[] { customerId, string.Format("customer{0}", customerId) } );
        
        // add 5 order records for each customer
        for( int i=1; i<=5; i++ )
        {
            myDataSet.Tables[ "Orders" ].Rows.Add( 
                new object[] { orderId++, customerId, orderId * 10 } );
        }
    }

    return myDataSet;
}

private static void PrintRowValues( DataRow[] rows, string label )
{
    Console.WriteLine( "\n{0}", label );
    if( rows.Length <= 0 )
    {
        Console.WriteLine( "no rows found" );
        return;
    }
    foreach( DataRow r in rows )
    {
        foreach( DataColumn c in r.Table.Columns )
        {
            Console.Write( "\t {0}", r[c] );
        }
        Console.WriteLine();
    }
}

[C++] 
private:
static void GetChildRowsFromDataRelation()
{
    /* 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. */
    DataTable* myTable = CreateDataSet()->Tables->Item[ S"Customers" ];
    DataRow* childRows[];
    System::Collections::IEnumerator* myEnum = myTable->ChildRelations->GetEnumerator();
    while (myEnum->MoveNext())
    {
        DataRelation* myRelation = __try_cast<DataRelation*>(myEnum->Current);
        System::Collections::IEnumerator* myEnum1 = myTable->Rows->GetEnumerator();
        while (myEnum1->MoveNext())
        {
            DataRow* myRow = __try_cast<DataRow*>(myEnum1->Current);

            DataRow* temp0 [] = {myRow};
            PrintRowValues( temp0, S"Parent Row" );
            childRows = myRow->GetChildRows(myRelation);
            // Print values of rows.
            PrintRowValues( childRows, S"child rows" );
        }
    }
}

public:
static DataSet* CreateDataSet()
{
    // create a DataSet with one table, two columns
    DataSet* myDataSet = new DataSet();

    // create Customer table
    DataTable* t = new DataTable( S"Customers" );
    myDataSet->Tables->Add( t );
    t->Columns->Add( S"customerId", __typeof(int) )->AutoIncrement = true;
    t->Columns->Add( S"name", __typeof(String) );

    DataColumn* temp3 [] = {t->Columns->Item[S"customerId"]};
    t->PrimaryKey = temp3;

    // create Orders table
    t = new DataTable( S"Orders" );
    myDataSet->Tables->Add( t );
    t->Columns->Add( S"orderId", __typeof(int) )->AutoIncrement = true;
    t->Columns->Add( S"customerId", __typeof(int) );
    t->Columns->Add( S"amount", __typeof(double) );

    DataColumn* temp4 [] = {t->Columns->Item[S"orderId"]};
    t->PrimaryKey = temp4;

    // create relation
    myDataSet->Relations->Add( myDataSet->Tables->Item[ S"Customers" ]->Columns->Item[ S"customerId" ],
        myDataSet->Tables->Item[ S"Orders" ]->Columns->Item[ S"customerId" ] );

    // populate the tables
    int orderId = 1;
    for( int customerId=1; customerId<=10; customerId++ )
    {
        // add customer record

        Object* temp1 [] = {__box(customerId), String::Format(S"customer{0}", __box(customerId))};
        myDataSet->Tables->Item[S"Customers"]->Rows->Add(
            temp1 );

        // add 5 order records for each customer
        for( int i=1; i<=5; i++ )
        {

            Object* temp2 [] = {__box(orderId++), __box(customerId), __box(orderId * 10)};
            myDataSet->Tables->Item[ S"Orders" ]->Rows->Add(
                temp2 );
        }
    }

    return myDataSet;
}

private:
static void PrintRowValues( DataRow* rows[], String* label )
{
    Console::WriteLine( S"\n{0}", label );
    if( rows->Length <= 0 )
    {
        Console::WriteLine( S"no rows found" );
        return;
    }
    System::Collections::IEnumerator* myEnum2 = rows->GetEnumerator();
    while (myEnum2->MoveNext())
    {
        DataRow* r = __try_cast<DataRow*>(myEnum2->Current);
        System::Collections::IEnumerator* myEnum3 = r->Table->Columns->GetEnumerator();
        while (myEnum3->MoveNext())
        {
            DataColumn* c = __try_cast<DataColumn*>(myEnum3->Current);
            Console::Write( S"\t {0}", r->Item[c] );
        }
        Console::WriteLine();
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataTable Class | DataTable Members | System.Data Namespace | ParentRelations | DataRelation | GetParentRows | GetChildRows

Show:
© 2015 Microsoft