Displaying Related Data
In this lesson, you will learn how to display related data in two separate tables on a Windows Form.
The previous four lessons describe how to create a database and modify and display data from it. When you install SQL Server Compact 3.5 with Visual Basic Express, you have access to a sample database named Northwind.sdf, which contains several tables.
To create and access a SQL Server Compact 3.5 database by using Visual Basic Express, you must first install SQL Server Compact 3.5. If you did not install it when you installed Visual Basic Express, you have to do so before continuing. For more information, see Using SQL Server Compact 3.5 (Visual Studio).
To connect to the Northwind database
On the File menu, click New Project.
In the New Project dialog box, in the Templates pane, click Windows Forms Application, and then click OK.
A new Windows Forms project opens.
On the Data menu, click Show Data Sources.
The Data Sources window opens.
In the Data Sources window, click Add New Data Source.
The Data Source Configuration Wizard opens.
Select Database and then click Next.
Click New Connection.
The Add Connection dialog box opens.
In the Add Connection dialog box, check whether the Data source is Microsoft SQL Server Compact 3.5 (.NET Framework Data Provider for Microsoft SQL Server Compact 3.5). If it isn't, click Change, select Microsoft SQL Server Compact 3.5 in the Change Data Source dialog box, and then click OK.
In the Add Connection dialog box, click Browse and locate the Northwind.sdf database in Program Files\Microsoft SQL Server Compact Edition\v 3.5\Samples, on the drive on which you installed SQL Server Compact 3.5.
Click OK to close the dialog box, and then, in the Data Source Configuration Wizard, click Next. If you are prompted to copy the data file to your project, click Yes.
On the next page of the wizard, the Yes, save the connection as check box should be selected. Click Next to continue.
On the Choose Your Database Objects page, expand the Tables node, and then select the check boxes for both the Order Details table and the Products table.
A local database file is added to your project. Notice that a NorthwindDataSet object appears in the Data Sources window.
In Solution Explorer, select the Northwind.sdf node and make sure that the Copy to Output Directory property is set to Copy if newer.
On the File menu, click Save All to save your project.
When there is related data in two tables, the relationship is visible in the Data Sources window. For example, if you expand the Products table, you will notice that the Order Details table is displayed as being part of the Products table. If you drag this Order Details table or any one of its fields to a Windows Form, you can show the relationship between the table or field and the Products table on the form. For example, you can move through the products in the Products table and display the corresponding order information for each product.
To display related data
In Solution Explorer, right-click Form1 and then click View Designer.
In Solution Explorer, click the Data Sources tab.
Expand the Products table, select Product Name, click the drop-down arrow next to Product Name, and then click Label.
Drag the Product Name field to the Windows Form.
Drag the Order Details table (the one that is located in the Products table) to the form, placing it under the label.
A DataGridView control is added to the form.
Press F5 to run the program.
When the application starts, click the Move Next button on the toolbar at the top of the form.
Verify that the order detail information in the DataGridView control is updated whenever the product changes.
In this lesson, you learned how to add data-bound controls to a Windows Form that navigates through related data. In the next lesson, you will learn about the O/R Designer.
Next Lesson: Creating LINQ to SQL Classes: Using the O/R Designer