Creating DataRelation Objects in Visual Studio .NET
Now that you understand the major features of the DataRelation object, let's see how to create DataRelation objects in Visual Studio .NET.
Adding a DataRelation to a Strongly Typed DataSet
After reading Chapter 6, you already know how to create a strongly typed DataSet in Visual Studio .NET. You double-click on the DataSet project item (*.xsd) in the Solution Explorer window to view the structure of your DataSet in the XML Schema Designer.
Right-click on the DataTable you want to use as the parent in the relationship, and choose Add, New Relation from the shortcut menu, as shown in Figure 7-7. You can also access this command from the Schema item on the Visual Studio .NET menu. You'll see a dialog box that you can use to add your new DataRelation, as shown in Figure 7-8.
Figure 7-7
Invoking the Edit Relation dialog box
Figure 7-8
Adding a new DataRelation in the Edit Relation dialog box
The Edit Relation dialog box lets you specify the parent and child DataTable and DataColumn objects for the new DataRelation. You can also specify values for the UpdateRule, DeleteRule, and AcceptRejectRule properties of the ForeignKeyConstraint that will be associated with the new DataRelation. If you want to create a ForeignKeyConstraint but not a DataRelation, you can select the Create Foreign Key Constraint Only check box.
Click OK, and you'll see a graphical representation of the new DataRelation in the XML Schema Designera link between the two DataTable objects, as shown in Figure 7-9.
Figure 7-9
The new DataRelation
Adding a DataRelation to an Untyped DataSet
You can also add DataRelation objects to untyped DataSet objects. In the previous chapter, you learned how to add an untyped DataSet to a designer, such as a Windows form, and then add DataTable objects and DataColumn objects to that new DataSet. Adding a new DataRelation is just as easy.
Select the DataSet in the designer's components tray, and then select the Relations property in the Properties window. You'll see a button to the right of the property that you can click to launch the Relations Collection Editor, as shown in Figure 7-10.
Figure 7-10
Adding a DataRelation to an untyped DataSet in the Relations Collection Editor
The Relations Collection Editor lets you add, edit, and remove DataRelation objects. If you choose to add a new DataRelation or edit an existing DataRelation, you'll see the Edit Relation dialog box shown earlier in Figure 7-8.