Partilhar via


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 a Relationship between Tables.

Relationships between DataTables appear as lines on the Dataset Designer with a key and infinity glyph depicting the one-to-many aspect of the relationship. By default the name of the relationship does not appear on the design surface.

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 Customizing Development Settings in Visual Studio.

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 Nested DataRelations.

  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.

To toggle the display of relation names in the Dataset Designer

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

  2. From the Data menu, select or clear the Show Relation Labels command to toggle displaying the relation name on or off.

See Also

Tasks

How to: Access Records in Related DataTables

Walkthrough: Creating a Relationship between Data Tables

Reference

GetChildRows

GetParentRows

DataRelation

Concepts

Introduction to DataRelation Objects

Working with Datasets in Visual Studio

Preparing Your Application to Receive Data

Editing Data in Your Application

Creating and Editing Typed Datasets

Preparing Your Application to Receive Data