Tài liệu Using the Data Form Wizard to Create a Windows Form phần 1 - Pdf 92

Using the Data Form Wizard to Create a Windows Form
In this section, you'll use the VS .NET Data Form Wizard to create a Windows
application that accesses both the Customers and Orders tables. The Orders table contains
rows that represent orders placed by the customers.
The rows in the Orders table are related to the rows in the Customers table through a
foreign key: The Orders table contains a column named CustomerID that is a foreign key
to the CustomerID column of the Customers table (CustomerID is the primary key for the
Customers table). The use of the foreign key defines a parent-child relationship between
the Customers and Orders tables.
The form you'll create will display a row from the Customers table, along with any
related rows from the Orders table. To give you a clear idea of the final goal, Figure 6.17

shows the completed form up and running. Notice that the top part of the form shows the
details for the row from the Customers table where the CustomerID is ALFKI; the bottom
part of the form contains a DataGrid control that displays the rows from the Orders table
for that customer. When you move to the next row in the Customers table, the rows from
the Orders table for that customer are automatically displayed in the DataGrid.

Figure 6.17: The running form
Perform these steps to begin build the form:
1. Select File ➣ New Project.
2. In the New Project dialog box, select Empty Project, and enter
DataFormWindowsApplication in the Name field. Because you'll be adding a
new form to your new application shortly, there's no need to have VS .NET
generate the usual blank form for you; that's why you're creating an empty project.
3. Click OK to continue. VS .NET will create a new empty project for you.
Next, you'll use the Data Form Wizard to create a form that accesses the Customers and
Orders tables in the Northwind database:
1. Select Project ➣ Add New Item.
2. Select Data Form Wizard from the Templates section on the right, enter the Name
of the form as MyDataForm.cs, and click Open (see Figure 6.18

table or view to your form by selecting it from the area on the left and clicking the right-
arrow button.
Tip You can also double-click on the table or view to add them to your form.
When you do this, the table or view moves to the right, indicating that you've selected
them for use in your form. If you decide you don't want to use a table or view, you can
unselect them using the left-arrow button. You can also double-click the table or view to
unselect them. Select the Customers and Orders tables, as shown in Figure 6.22
. Click the
Next button to proceed.

Figure 6.22: Selecting the Customers and Orders tables for use in the form
Because you selected two tables-Customers and Orders-your next step is to define a
relationship between those tables. This relationship is used in your form to synchronize
navigation between the rows in the Customers table with the rows in the Orders table:
When you move to a new row in the Customers table, the rows from the Orders table will
be displayed in your form. Set the following in the dialog box (as shown in Figure 6.23
):
1. Enter myRelationship in the Name field.
2. Select Customers as the parent table.
3. Select Orders as the child table.
4. Select CustomerID as the key for each table.
Warning To add the relationship to your form, click the right-arrow button. If you
don't do this, your relationship won't be added to your form.
5. Click the Next button to continue.
6. Select the columns from the tables you want to display in your form. Because you
added the Customers and Orders tables to your form, you'll be selecting the
columns to display from these two tables. By default, all the columns from the
tables are selected. You won't be displaying all the columns from the Customers or
the Orders table. Unselect the City column for the Customers table. (Later, you'll
see how to add this column to your form manually.)


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status