Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

Grid, Column, Header

A grid is a “browse-in-a-read” or at least the VFP equivalent. Columns and Headers are the stuff that grids are made of.

A grid is a container object. It contains columns. Columns are also containers. They contain headers and controls. Each column has only one header, but can contain as many controls as you like. By default, each column starts out with a single text box.

Grids display table, view, or cursor contents. Each row of the grid reflects a single record. When you access the control in a column, it’s bound to the appropriate field of the current record (or an expression based on the current record).

Grid

Property

Value

Purpose

ActiveColumn, ActiveRow

Numeric

Contain the number of the column and row that have focus, respectively. Measured in absolute terms from the first row and column of the grid. Uses ColumnOrder for columns, not creation order.

ChildOrder

Character

When the grid is the "many" side of a one-to-many relation, can indicate the name of the index tag by which the table is ordered. (We say "can" because you can also do this using Data Environment properties.)

ColumnCount

Numeric

The number of columns in the grid. Set it to -1 to have one column for each field in the current RecordSource.

Columns

Collection

References to the columns in the grid.

DeleteMark

Logical

Determines whether the grid has a "delete mark" column, allowing the user to click to delete or recall a record.

GridLineColor

Numeric

The color in which the grid lines appear.

GridLines

Numeric

Determines whether the grid has horizontal or vertical grid lines, neither or both.

GridLineWidth

Numeric

The thickness of the grid lines.

HeaderHeight

Numeric

The height of the headers in the grid.

Highlight

Logical

Determines whether the contents of the cell with focus are highlighted when you tab into the cell.

HighlightRow

Logical

Determines whether the current cell is highlighted.

HighlightRowLineWidth

Numeric

New to VFP 7, this determines the width of a box highlighting the current row. Acceptable values are 0–7. HighlightRow must be .T. to use this property.

LeftColumn

Numeric

Contains the ColumnOrder of the leftmost column currently visible in the grid.

LinkMaster

Character

When the grid is the "many" side of a one-to-many relation, can indicate the alias of the "one" table.

Objects

Collection

References to the columns in the grid.

Panel

Numeric

Determines which side of a split grid has the focus.

PanelLink

Logical

Determines whether the two sides of a split grid are linked.

Partition

Numeric

Determines (in pixels) where the divider for a split grid appears. When 0, the grid isn't split.

RecordMark

Logical

Determines whether the grid has a column (on the left border) that contains a pointer for the current record.

RecordSource

Character

The table name, alias or filename of the table or query that provides the data for the grid.

RecordSourceType

Numeric

The type of object that provides the data for the grid.

RelationalExpr

Character

When the grid is the "many" side of a one-to-many relation, can indicate the expression that links the two tables.

RelativeColumn, RelativeRow

Numeric

The column and row of the cell with focus, relative to the cell visible in the upper-left corner.

RowColChange

Numeric

Contains a value indicating whether the row, column, or both changed when moving into the current cell.

RowHeight

Numeric

The height of the rows in the grid.

Scrollbars

Numeric

Determines what scrollbars, if any, the grid has.

Event

Purpose

AfterRowColChange

Fires after focus has moved to a new cell.

BeforeRowColChange

Fires when the user attempts to move focus to a new cell.

Scrolled

Fires when the user uses the scrollbars.

Method

Purpose

ActivateCell

Set focus to a particular cell in the grid.

AddColumn, RemoveColumn

Add and remove columns from the grid.

DoScroll

Scroll the grid in a specified direction.

GridHitTest

Return information about the grid element at a specified point.

Column

Property

Value

Purpose

Bound

Logical

Determines whether the controls in the column are bound to the column's ControlSource or their own.

ColumnOrder

Numeric

Determines the display position of the column within the grid.

ControlCount

Numeric

The number of controls in the column.

Controls

Collection

References to the controls in the column, including the header.

CurrentControl

Character

The name of the control in the column that is presented to the user.

DynamicAlignment

Character

An expression that evaluates to a legal Alignment value and indicates the alignment for each cell in this column at this time.

DynamicBackColor, DynamicForeColor

Character

Expressions that evaluate to color values indicating the colors used for this column at this time. Evaluated individually for each cell in the column each time the cell is redrawn.

DynamicCurrentControl

Character

An expression that evaluates to the name of one of the column's controls. Indicates which control the user should see.

DynamicFontBold, DynamicFontItalic, DynamicFontName, DynamicFontSize, DynamicFontStrikeThru, DynamicFontUnderline

Character

Expressions that evaluate to appropriate font property values. Indicate which font characteristics the column should have at this time. Like other Dynamic... properties, these are evaluated each time the grid is refreshed.

DynamicFontOutline, DynamicFontShadow

Character

Do nothing except in VFP/Mac.

DynamicInputMask

Character

An expression that evaluates to a valid input mask, indicating the mask to be used for this column at this time. Evaluated each time the grid is refreshed.

Movable

Logical

Determines whether the user can move the column.

Objects

Collection

References to the controls in the column, including the header.

Resizable

Logical

Determines whether the user can resize the column.

SelectOnEntry

Logical

Determines whether the contents of the column are highlighted when the user tabs into it.

Sparse

Logical

Determines whether the current control for the column shows all the time or only when the cell has focus.

Up until VFP 7, headers had no unique or unusual properties, events or methods, just the basic ones shared by many classes. WordWrap is perhaps one of the most heralded enhancements, allowing multi-line grid headers.

One of the trickiest issues with grids is figuring out which object in a grid receives events, especially mouse events. The grid receives these events only if nothing it contains is able to. If you click in a control of a column, the control’s Click is executed. If you click on the header of a column, the header’s Click fires. To fire the grid’s Click, you have to click on a row after the last record, or on the record mark or delete mark.

VFP 7 Service Pack 1 fixes a bug introduced in VFP 7, where the Click and other events are called only if the grid's RecordSource contains records.

Like other objects designed only to be contained in a particular container, columns and headers can’t be subclassed in the Class Designer. (See PageFrame for the consequences of this.) A good builder can be useful here, as can putting some code in the grid class’s Init method.

Aside from all other differences, you can build grids two different ways depending on the value of ColumnCount. When ColumnCount is –1, the grid contains one column for each field of the RecordSource. In this case, you can change column properties in code using the Columns collection, but you can’t do it in the property sheet. When ColumnCount is positive, you can set up each specified column ahead of time.

Example

* Set up a grid as the child side of the relation between
* the TasTrade Orders and Order_Line_Items tables.
* Assume both tables are in the Data Environment, but that
* the relation between them has been removed. Usually, you
* wouldn't do it this way - you'd use the relation in the DE.
* Set these grid properties as shown:
RecordSource = "Order_Line_Items"
ChildOrder = "Order_Id"
LinkMaster = "Orders"
RelationalExpr = "Order_Id"

* Now let's put only the columns we want to see:
ColumnCount = 3

* Now switch to column properties.
* For column1.header1:
Caption = "Product Id"

* For column1:
ControlSource = "Order_Line_Items.Product_Id"

* For column2.header1:
Caption = "Unit Price"

* For column2:
ControlSource = "Order_Line_Items.Unit_Price"

* For column3.header1:
Caption = "Quantity"

* For column3:
ControlSource = "Order_Line_Items.Quantity"

* Now set the column up to use blue for any item where quantity
* is more than 10. For column3:
DynamicForeColor = "IIF(Order_Line_Items.Quantity>10, ;
   RGB(0,0,255),RGB(0,0,0))"

* You'll want to do more cosmetic stuff like resizing the
* columns and so forth.

See Also

ActivateCell, ActiveColumn, ActiveRow, AddColumn, AfterRowColChange, BeforeRowColChange, Bound, ChildOrder, ColumnCount, Columns, ColumnOrder, Controls, ControlCount, CurrentControl, DeleteColumn, DeleteMark, DoScroll, DynamicAlignment, DynamicBackColor, DynamicCurrentControl, DynamicFontBold, DynamicFontItalic, DynamicFontName, DynamicFontOutline, DynamicFontShadow, DynamicFontSize, DynamicFontStrikeThru, DynamicFontUnderline, DynamicForeColor, DynamicInputMask, GridLineColor, GridLines, GridLineWidth, GridHitTest, HeaderHeight, Highlight, HighlightRow, HighlightRowLineWidth, LeftColumn, LinkMaster, Movable, Objects, Panel, PanelLink, Partition, RecordMark, RecordSource, RecordSourceType, RelationalExpr, RelativeColumn, RelativeRow, Resizable, RowColChange, RowHeight, Scrollbars, Scrolled, SelectOnEntry, Sparse, WordWrap