共用方式為


Setting Up a One-to-Many Form Using the Grid Control

One of the most common uses for a grid is to display the child records for a table while text boxes display the data for the parent records. When the user moves through the records in the parent table, the grid displays the appropriate child records.

If you have a data environment for your form that includes a one-to-many relationship between two tables, displaying the one-to-many relationship in the form is very easy.

To set up a one-to-many form with a data environment

  1. Drag desired fields from the parent table in the Data Environment Designer to your form.
  2. Drag the related table from the Data Environment Designer to the form.

In almost all cases, you'll want to create a data environment for your form or form set. However, it's not much more complicated to create a one-to-many form without using the Data Environment Designer.

To set up a one-to-many form without creating a data environment

  1. Add text boxes to your form to display the desired fields from the primary table.
  2. Set the ControlSource property of the text boxes to the primary table.
  3. Add a grid to the form.
  4. Set the RecordSource property of the grid to the name of the related table.
  5. Set the LinkMaster property of the grid to the name of the primary table.
  6. Set the ChildOrder property of the grid to the name of the index tag in the related table that corresponds to the relational expression of the primary table.
  7. Set the RelationalExpr property of the grid to the expression that joins the related table to the primary table. For example, if the ChildOrder tag is indexed on "lastname + firstname", set RelationalExpr to the same expression.

Either way you set up the one-to-many form, you can add navigation controls to move through the parent table and refresh the form objects. For example, the following code could be included in the Click event of a command button:

SELECT orders && if orders is the parent table
SKIP
IF EOF( )
   GO BOTTOM
ENDIF
THISFORM.Refresh

See Also

Setting the Source of the Data Displayed in the Grid | Displaying Controls in Grid Columns | Using Controls | Making Controls Easier to Use | Controls and Objects