Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
10 out of 14 rated this helpful - Rate this topic

DataRelation Class

Represents a parent/child relationship between two DataTable objects.

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

public class DataRelation
public class DataRelation
public class DataRelation

A DataRelation is used to relate two DataTable objects to each other through DataColumn objects. For example, in a Customer/Orders relationship, the Customers table is the parent and the Orders table is the child of the relationship. This is similar to a primary key/foreign key relationship. For more information, see Navigating a Relationship Between Tables.

Relationships are created between matching columns in the parent and child tables. That is, the DataType value for both columns must be identical.

Relationships can also cascade various changes from the parent DataRow to its child rows. To control how values are changed in child rows, add a ForeignKeyConstraint to the ConstraintCollection of the DataTable object. The ConstraintCollection determines what action to take when a value in a parent table is deleted or updated.

When a DataRelation is created, it first verifies that the relationship can be established. After it is added to the DataRelationCollection, the relationship is maintained by disallowing any changes that would invalidate it. Between the period when a DataRelation is created and added to the DataRelationCollection, it is possible for additional changes to be made to the parent or child rows. An exception is generated if this causes a relationship that is no longer valid.

DataRelation objects are contained in a DataRelationCollection, which you can access through the Relations property of the DataSet, and the ChildRelations and ParentRelations properties of the DataTable.

The following example creates a new DataRelation and adds it to the DataRelationCollection of a DataSet.

private void CreateRelation() 
{
    // Get the DataColumn objects from two DataTable objects 
    // in a DataSet. Code to get the DataSet not shown here.
    DataColumn parentColumn = 
        DataSet1.Tables["Customers"].Columns["CustID"];
    DataColumn childColumn = 
        DataSet1.Tables["Orders"].Columns["CustID"];
    // Create DataRelation.
    DataRelation relCustOrder;
    relCustOrder = new DataRelation("CustomersOrders", 
        parentColumn, childColumn);
    // Add the relation to the DataSet.
    DataSet1.Relations.Add(relCustOrder);


System.Object
  System.Data.DataRelation

This type is safe for multithreaded read operations. You must synchronize any write operations.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.