How To Create a Calculated Column in a Grid

Last reviewed: October 18, 1996
Article ID: Q138981
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, versions 3.0, 5.0

SUMMARY

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

MORE INFORMATION

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.

  1. Create a new form.

  2. 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.

  3. Drag the table by its title bar from the data environment onto the form to create a grid.

  4. Set the Grid.ColumnCount property to 3.

  5. Set the ControlSource property for each column as follows:

    Column1.ControlSource = unit_price Column2.ControlSource = quantity Column3.ControlSource = unit_price * quantity

  6. Save and run the form.

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


Additional reference words: 5.00 3.00 VfoxWin
KBCategory: kbprg kbcode kbhowto
KBSubcategory: FxprgGrid


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 18, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.