Walkthrough: Creating a Relationship between Data Tables
This walkthrough explains how to create two data tables without TableAdapters using the Dataset Designer and creating a relationship between them. For information on creating data tables that include TableAdapters, see How to: Create TableAdapters. For more information on TableAdapters, see TableAdapter Overview.
Tasks illustrated in this walkthrough include:
-
Creating a new Windows application
-
Adding a new dataset to the application
-
Adding two new data tables to the dataset
-
Adding columns to the data tables
-
Setting the primary key for the tables
-
Creating a relationship between the tables
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 new Windows Application project
-
From the File menu, create a new project.
-
Select a programming language in the Project Types pane and create a Windows Application.
-
Name the project RelationshipWalkthrough, and then click OK.
Visual Studio will add the project to Solution Explorer and display Form1 in the Windows Forms Designer.
To add a new data table to the dataset
-
Drag a DataTable from the DataSet tab of the Toolbox onto the Dataset Designer.
A table named DataTable1 is added to the dataset.
-
Click the title bar of DataTable1 and rename it Customers.
-
Drag a second DataTable from the DataSet tab of the Toolbox onto the Dataset Designer.
A table named DataTable1 is added to the dataset.
-
Click the title bar of DataTable1 and rename it Orders.
The relationship is created between the common column from each table — in this case, the CustomerID column.
To configure a new relationship between the Customers and Orders tables
-
Drag a Relation object from the DataSet tab of the Toolbox onto the Orders table.
The Relation dialog box opens.
-
In the Parent Table box, select Customers.
-
In the Child Table box, select Orders.
-
In the Columns box, set Key Columns to CustomerID.
-
In the Columns box, set Foreign Key Columns to CustomerID.
-
Click OK to create the relationship; a relation line appears on the designer between the two tables.
-
On the Data menu, choose Show Relation Labels.
Note