Definition of a Grid Control

A grid contains columns, which in turn can contain headers and any other control. The default control contained in a column is a text box, so that the default functionality of the grid approximates a Browse window. However, the underlying architecture of the grid opens it up to endless extensibility.

The following example creates a form that contains a Grid object with two columns. The second column contains a check box to display the values in a logical field in a table.

Grid control with a check box in one column

Definition of a Grid Class with a Check Box in a Grid Column

Code Comments
DEFINE CLASS grdProducts AS Grid
   Left = 24
   Top = 10
   Width = 295
   Height = 210
   Visible = .T.
   RowHeight = 28
   ColumnCount = 2
Start the class definition and set properties that determine the grid appearance.

When you set the ColumnCount property to 2, you add two columns to the grid. Each column contains a header with the name Header1. In addition, each column has an independent group of properties that determines its appearance and behavior.
Column1.ControlSource ="prod_name"
Column2.ControlSource ="discontinu"
When you set the ControlSource of a column, the column displays that field's values for all the records in the table.
Discontinu is a logical field.
Column2.Sparse = .F.
Column2 will contain the check box. Set the column's Sparse property to .F. so that the check box will be visible in all rows, not just in the selected cell.
Procedure Init
   THIS.Column1.Width = 175
   THIS.Column2.Width = 68
   THIS.Column1.Header1.Caption = ;
      "Product Name"
   THIS.Column2.Header1.Caption = ;
      "Discontinued"
   THIS.Column2.AddObject("chk1", ;
      "checkbox")
   THIS.Column2.CurrentControl = ;
      "chk1"
   THIS.Column2.chk1.Visible = .T.
   THIS.Column2.chk1.Caption = ""
ENDPROC
Set column widths and header captions.







The AddObject method allows you to add an object to a container — in this case, a check box named chk1.
Set the CurrentControl of the column to the check box so that the check box will be displayed.
Make sure that the check box is visible.
Set the caption to an empty string so that the default caption "chk1" won't be displayed.
ENDDEFINE
End of the class definition.

The following class definition is the form that contains the grid. Both class definitions can be included in the same program file.

Definition of a Form Class that Contains the Grid Class

Code Comments
DEFINE CLASS GridForm AS FORM
   Width = 330
   Height = 250
   Caption = "Grid Example"
   ADD OBJECT grid1 AS grdProducts
Create a form class and add an object, based on the grid class, to it.
PROCEDURE Destroy
   CLEAR EVENTS
ENDPROC
ENDDEFINE
The program that creates an object based on this class will use READ EVENTS. Including CLEAR EVENTS in the Destroy event of the form allows the program to finish running when the user closes the form.
End of the class definition.

The following program opens the table with the fields to be displayed in the grid columns, creates an object based on the GridForm class, and issues the READ EVENTS command:

CLOSE DATABASE
OPEN DATABASE (HOME(2) + "data\testdata.dbc")
USE products
frmTest= CREATEOBJECT("GridForm")
frmTest.Show
READ EVENTS

This program can be included in the same file with the class definitions if it comes at the beginning of the file. You could also use the SET PROCEDURE TO command to specify the program with the class definitions and include this code in a separate program.

See Also

Writing Class Definitions Programmatically | Creating a Set of Table Navigation Buttons | Object Reference Creation | Browse | Sparse property | AddObject method | CurrentControl | Object-Oriented Programming