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.

DataRelation Class

Represents a parent/child relationship between two DataTable objects.

For a list of all members of this type, see DataRelation Members.

System.Object
   System.Data.DataRelation

[Visual Basic]
<Serializable>
Public Class DataRelation
[C#]
[Serializable]
public class DataRelation
[C++]
[Serializable]
public __gc class DataRelation
[JScript]
public
   Serializable
class DataRelation

Thread Safety

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

Remarks

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 results in 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.

Example

[Visual Basic, C#, C++] The following example creates a new DataRelation and adds it to the DataRelationCollection of a DataSet.

[Visual Basic] 
Private Sub CreateRelation()
    ' Get the DataColumn objects from two DataTable objects in a DataSet.
    Dim parentCol As DataColumn
    Dim childCol As DataColumn
    ' Code to get the DataSet not shown here.
    parentCol = DataSet1.Tables("Customers").Columns("CustID")
    childCol = DataSet1.Tables("Orders").Columns("CustID")
    ' Create DataRelation.
    Dim relCustOrder As DataRelation
    relCustOrder = New DataRelation("CustomersOrders", parentCol, childCol)
    ' Add the relation to the DataSet.
    DataSet1.Relations.Add(relCustOrder)
 End Sub

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

[C++] 
private:
void CreateRelation() {
    // Get the DataColumn objects from two DataTable objects in a DataSet.
    DataColumn* parentCol;
    DataColumn* childCol;
    // Code to get the DataSet not shown here.
    parentCol = DataSet1->Tables->Item[S"Customers"]->Columns->Item[S"CustID"];
    childCol = DataSet1->Tables->Item[S"Orders"]->Columns->Item[S"CustID"];
    // Create DataRelation.
    DataRelation* relCustOrder;
    relCustOrder = new DataRelation(S"CustomersOrders", parentCol, childCol);
    // Add the relation to the DataSet.
    DataSet1->Relations->Add(relCustOrder);
 }

[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

Namespace: System.Data

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

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

See Also

DataRelation Members | System.Data Namespace | ChildRelations | DataColumn | DataSet | ForeignKeyConstraint | DataRelationCollection | UniqueConstraint | ParentRelations

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft