Microsoft KB Archive/138981

= How To Create a Calculated Column in a Grid =

Article ID: 138981

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q138981



SUMMARY
You can create a calculated column in a grid by entering the calculation in the ControlSource property for the column.



Step-by-Step Example
The following steps create a form containing a simple grid. The third column of the grid is the product of the values in the first two columns.

 Create a new form. In Visual FoxPro 3.0, add the Order_Line_Items table from the Tastrade.dbc database located in the \Vfp\Samples\Mainsamp\Data directory to the data environment of the form.

-or-

In Visual FoxPro 5.0, add the Order_Line_Items table from the Tastrade.pjx database located in the \Vfp\Samples\Tastrade directory to the data environment of the form. Drag the table by its title bar from the data environment onto the form to create a grid. Set the Grid.ColumnCount property to 3.  Set the ControlSource property for each column as follows: Column1.ControlSource = unit_price Column2.ControlSource = quantity Column3.ControlSource = unit_price * quantity  Save and run the form.</ol>

The third column will show the extended price (unit price times quantity) for each record in the grid.

The grid will not automatically update a field in the table because the ControlSource is not bound to a specific field. If you need to store the calculation in a field, add the following code to the AfterRowColChange event, or to the record validation in the Database Designer: REPLACE fieldname WITH unit_price * quantity

Keywords: kbhowto kbcode KB138981

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.