Article ID:qFRM002
Date Revised:October 17, 1997
Keywords:parent-child grid, parameterized view, view, grid
See Also:Click to download the parchild.zip sample Microsoft White Paper Views.exe  KB Article Q142974 KB Article Q130707  

Question: How do you show child records in a grid?

Answer: Given 2 tables:

df_parent
   iID     i
   cName   c(10)

df_child
   iID     i
   iParent i
   cName   c(10)

Where the relation between the tables is:

   df_child.iParent = df_parent.iID

One way to accomplish this is to use the ChildOrder, LinkMaster, and RelationalExpr properties of the grid control. Set ChildOrder to the foreign key field into the parent table iParent, LinkMaster to the name of the parent table df_parent and the RelationalExpr to the relationship child.ForeignKeyField=parent.KeyField df_child.iParent = df_parent.iID. Parchild.scx of the parchild.zip illustrates the grid property setting technique.

NOTE: You should make sure the parent table is the InitialSelectedAlias of the DataEnvironment. If the child table is selected instead of the parent all of the child records will display.

Another way to do this would be to create a parameterized view of the child table. And use the view as the RowSource of the grid. As the parent record changes you just Requery the view. This also has the benefit of being able to more easily change the order of the grid records without resorting to concatenated keys. Parview.scx of the parchild.zip illustrates the view technique.

The view looks like:

SELECT *;
   FROM df_pa_ch!df_child;
   WHERE df_child.iParent = ?df_parent.iID;
   ORDER BY df_child.cName

NOTE: When you create the view in the View Designer do not specify a Join condition, just set the ? parameter as the Filter condition. A join will cause all of the child records to display.


1