Walkthrough: Displaying Related Data on a Form in a Windows Application
In many application scenarios, you want to work with data that comes from more than one table and, often, data from related tables. That is, you want to work with a parent-child relationship. For example, you might want to create a form where selecting a customer record displays the orders for that customer. Displaying the related records on the form is achieved by setting the DataSource property of the child BindingSource to the parent BindingSource (not the child table), and setting the DataMember property of the child BindingSource to the data relation that ties the parent and child tables together.
Tasks illustrated in this walkthrough include:
Creating a Windows Application project.
Creating and configuring a dataset in your application based on the Customers and Orders tables in the Northwind database using the Data Source Configuration Wizard.
Adding controls to display data from the Customers table.
Adding controls to display the Orders based on the selected Customer.
Testing the application by selecting different customers and verifying that the correct orders are displayed for the selected customer.
In order to complete this walkthrough, you need:
Access to the Northwind sample database. To setup sample databases, see How to: Install Sample Databases.
The first step is to create a Windows Application.
To create the Windows Application project
From the File menu, create a new project.
Name the project RelatedDataWalkthrough.
Select Windows Application and click OK. For more information, see Creating Windows-Based Applications.
The RelatedDataWalkthrough project is created and added to Solution Explorer.
This step creates a dataset based on the Customers and Orders tables in the Northwind sample database.
To create the data source
On the Data menu, click Show Data Sources.
In the Data Sources window, select Add New Data Source to start the Data Source Configuration Wizard.
Select Database on the Choose a Data Source Type page, and then click Next.
On the Choose your Data Connection page do one of the following:
If a data connection to the Northwind sample database is available in the drop-down list, select it.
Select New Connection to launch the Add/Modify Connection dialog box. For more information, see Add/Modify Connection Dialog Box (General).
If your database requires a password, select the option to include sensitive data, and then click Next.
Click Next on the Save connection string to the Application Configuration file page.
Expand the Tables node on the Choose your Database Objects page.
Select the Customers and Orders tables, and then click Finish.
The NorthwindDataSet is added to your project and the Customers table appears in the Data Sources window.
To create controls to display the customer data (parent records)
In the Data Sources window, select the Customers table, and then click the drop-down arrow.
Choose Details from the menu.
Drag the main Customers node from the Data Sources window onto the top of Form1.
Data-bound controls with descriptive labels appear on the form, along with a tool strip (BindingNavigator) for navigating records. A NorthwindDataSet, CustomersTableAdapter, BindingSource, and BindingNavigator appear in the component tray.
To create controls to display the orders for each customer (child records)
In the Data Sources window, expand the Customers node and select the last column in the Customers table, which is an expandable Orders node, and drag it onto the bottom of Form1.
Open the Properties Window and select the OrdersBindingSource. Inspect the DataSource and DataMember properties to see how binding is configured to display related records. The DataSource is set to the CustomersBindingSource (the parent table's BindingSource), rather than the Orders table. The DataMember property is set to FK_Orders_Customers, which is the name of the DataRelation object that relates the tables together.
To test the application
Press F5 to run the application.
Select different customers using the CustomersBindingNavigator to verify the correct orders are displayed in the DataGridView.
Depending on your application requirements, there are several steps you may want to perform after creating a master-detail form. One enhancement you could make to this walkthrough is:
Filtering the Customers records by adding parameterization to the Customers table. To do this, select any control that displays data from the Customers table, click the smart tag, and choose Add Query. Complete the Search Criteria Builder Dialog Box. For more information, see How to: Add a Parameterized Query to a Form in a Windows Application.