Section 5.3. Create Master Detail Records


5.3. Create Master Detail Records

The Orders table is great, but when you look at an order, you want to look at the details of that order as well. The Northwind database establishes a parent-child relationship between each record in the Orders table and one or more orders in the Order Details table. You can reflect that in your application, again with no code.

5.3.1. How do I do that?

Begin by creating a copy of the previous project (using Windows Explorer) and renaming it MasterDetails. Change the name of the solution, the project, and the form.

Open the Data Sources view, right-click NorthwindDataSet, and choose Configure DataSet with Wizard, as shown in Figure 5-20.

Figure 5-20. Configuring the Northwind data set


Open the Tables in the Data Set, add the Order Details table, and click Finish.

The data set is updated with relation objects based on the appropriate columns; you do not have to write the code to create this relation. Return to the form and reposition half of the controls to the upper half of the form to make room for displaying the order details.

Expand the Orders table. The last node is an OrderDetails node. Drag that onto the form to create a data grid. Hey! Presto! Instant master/detail records!

5.3.2. What just happened?

Not only was the grid added to your form, but two new controls were added as well: order_DetailsBindingSource and order_detailsTableAdapter.

In addition, code is added to your .cs file to manage filling the two components: one for the order and the other for the order details, as shown in Example 5-2.

Example 5-2. Code added by the Designer
private void Form1_Load(object sender, EventArgs e) {     this.order_DetailsTableAdapter.Fill (this.northwindDataSet.Order_Details);     this.ordersTableAdapter.Fill(this.northwindDataSet.Orders);      }

Enter the customer ID and run the application. Not only is the order shown, but all the related data from the Order Details table is automatically synchronized to the appropriate order, as shown in Figure 5-21.

Figure 5-21. Master detail implemented with no code


5.3.3. What about...

...if I want to change the appearance of the details?

You have numerous options. The easiest is to click the smart tag for the grid and then to click Auto Format, as shown in Figure 5-22.

Figure 5-22. Changing the appearance of the details


In addition, you can add or remove columns or change the headers of the columns through the smart tag. You can set other properties through the properties window, and you can respond to dozens of events to interrupt the process and massage either the data or its display in any way you want.

5.3.4. Where can I learn more?

Try the walkthrough "Displaying Related Data on a Windows Form" on the MSDN site.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net