How to: Create DataRelations with the Dataset Designer

Relationships between data tables use DataRelation objects to connect records that are associated with each other, such as a customer and their orders. When you create data tables with the data design tools in Visual Studio, relationships are created automatically if the information can be gathered from the source of your data. If you manually add data tables from the DataSet tab of the Toolbox, you may have to create the relationship manually as explained on this Help page. For information on creating DataRelation objects programmatically, see Adding DataRelations (ADO.NET).

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To create a relationship between two DataTables

  1. Open your dataset in the Dataset Designer. For more information, see How to: Open a Dataset in the Dataset Designer.

  2. Drag a Relation object from the DataSet toolbox onto the child data table in the relationship.

    The Relation Dialog Box opens, populating the Child Table box with the table you dragged the Relation onto.

  3. Select the parent table from the Parent Table box. The parent table contains records on the "one" side of a one-to-many relationship.

  4. Verify the correct child table is displayed in the Child Table box. The child table contains records on the "many" side of a one-to-many relationship.

  5. Type a name for the relationship in the Name box, or leave the default name based on the selected tables. This is the name of the actual DataRelation object in code.

  6. Select the columns that join the tables in the Key Columns and Foreign Key Columns lists.

  7. Select whether to create a relation, constraint, or both. For information, see Introduction to DataRelation Objects.

  8. Select or clear the Nested Relation box. Selecting this option sets the Nested property to true, and it causes the child rows of the relation to be nested within the parent column when written as XML data or synchronized with an XmlDataDocument. For more information, see Nesting DataRelations (ADO.NET).

  9. Set the rules to be enforced when making changes to records in these tables. For more information, see Rule.

  10. Click OK to create the relationship; a relation line appears on the designer between the two tables. You can toggle showing the relation name on the design surface by selecting or clearing Show Relation Labels on the Data menu.

See Also

Tasks

How to: Access Records in Related DataTables

Walkthrough: Creating a Relationship between Data Tables

Reference

GetChildRows

GetParentRows

Concepts

Introduction to DataRelation Objects

Working with Datasets in Visual Studio

Preparing Your Application to Receive Data

Editing Data in Your Application

Dataset Designer