How to: Display Related Data on a Form

Data in separate tables can be related. When related data is in two tables, the relationship is visible in the Data Sources window. For example, if you connect to the sample Northwind database and expand the Products table, you will see that an Order Details table is displayed as part of the Products table. If you drag this Order Details table or one or more of its fields to a Windows Form, it is displayed as part of a relationship with the Products table. For example, if you scroll through the products in the Products table, the corresponding order information for each product is automatically displayed.

  1. On the File menu, click New Project.

  2. In the New Project dialog box, click Windows Application, and then click OK.

    A new Windows Forms project opens.

  3. Add a connection to the sample database named Northwind.sdf as described in How to: Connect to a Database (C#).

  4. On the Data menu, click Show Data Sources.

    The Data Sources window opens.

  5. In the Data Sources window, expand NorthwindDataSet, and then expand Products.

  6. Click the drop-down arrow next to Product Name, and then click Label.

  7. Drag the Product Name field to the Windows Form.

  8. Drag the Order Details table (the one that is located in the Products table) to the form, and position it under the label.

    A DataGridView control is added to the form.

  9. Press F5 to run the program.

  10. When the application opens, click the Move Next Item button on the ProductsBindingNavigator at the top of the form.

  11. Verify that the order detail information in the DataGridView control is updated whenever the product changes.

See Also

Tasks

How to: Connect to a Database (C#)

How to: Display Data on a Form

Other Resources

Connecting to Data (Visual C#)

Visual C# Guided Tour

SQL Server Compact 3.5 Books Online