This documentation is archived and is not being maintained.

DataTable.ChildRelations Property

Gets the collection of child relations for this DataTable.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

public DataRelationCollection ChildRelations { get; }

Property Value

Type: System.Data.DataRelationCollection
A DataRelationCollection that contains the child relations for the table. An empty collection is returned if no DataRelation objects exist.

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.

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 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 table = CreateDataSet().Tables["Customers"];
        DataRow[] childRows;
        foreach(DataRelation relation in table.ChildRelations)
            foreach(DataRow row in table.Rows)
                PrintRowValues(new DataRow[] {row}, "Parent Row");
                childRows = row.GetChildRows(relation);
                // Print values of rows.
                PrintRowValues(childRows, "child rows");

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

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

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

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

        return dataSet;

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0