DOCERR: How to Set Column Index Correctly for AddColumn Method

Last reviewed: December 6, 1995
Article ID: Q135456
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0

SUMMARY

The Visual FoxPro Language Reference and Help file both list the following inaccurate information under the topic for the AddColumn method:

   The existing Columns are moved to the right and are incremented by 1.

Although the columns do move to the right on the screen, the column index is not incremented.

NOTE: The Visual FoxPro 3.0b for Windows documentation is correct.

MORE INFORMATION

The column index is an internal array maintained by the grid object. Each column is assigned a subscript within the array. New columns are appended to the end of this array, and the array element number becomes the index for the column.

The name of the column is determined by concatenating the word "Column" plus the index number converted to a string.

If the AddColumn method is used to insert a column between columns 2 and 3 of a grid containing four columns, the name of the new column will be Column5. This can be confusing when it is necessary to refer to the columns later on because the column name does not reflect its position within the grid.

If the Form.SaveAs method is used to save the form and the form is modified, the new column will appear at the end of all columns.

Step-by-Step Procedure

The following method can be used to add a new column, set the order appropriately, and change the name of each column to reflect the new sequential order. If the Form.SaveAs method is used to save the form, the form will reappear with the columns in the desired order.

  1. Create a new form, and add the C:\VFP\Samples\Mainsamp\Data\Customer.dbf table to the data environment of the form.

  2. Add a grid to the form.

  3. On the Form menu, click New Method. Name the method grdAddColumn and input the following code for the method:

    PARAMETERS lcMyGridName, lnNewColumnPosition

          lnExistingColumns=ThisForm.&lcMyGridName..ColumnCount
          IF lnExistingColumns = -1
    
             lnExistingColumns=FCOUNT()
          ENDIF
          lcLastColumn = "Column" + ALLTRIM(STR(lnExistingColumns+1))
          ThisForm.&lcMyGridName..AddColumn(lnNewColumnPosition)
          ThisForm.&lcMyGridName..&lcLastColumn..ColumnOrder ;
             = lnNewColumnPosition
          FOR I = 1 TO lnExistingColumns + 1
             thisform.&lcMyGridName..Columns(i).Name = ;
                "Column" + ALLTRIM(STR(i))
          ENDFOR
          ThisForm.&lcMyGridName..Refresh
    
    

  4. Add a command button to the form, and put the following code in its Click event procedure:

          frmAdd.grdAddColumn("grid1", 2)
    

  5. Save the form as frmAdd, and run it.

A new column will be inserted as the second column each time you click the the command button. All column names will be changed to reflect their position within the grid.


Additional reference words: 3.00 VFoxWin
KBCategory: kbprg kbdocerr kbcode
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: December 6, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.