Share via


Displaying Controls in Grid Columns

In addition to displaying field data in a grid, you can have controls in the columns of a grid so that you can present a user with embedded text boxes, check boxes, drop-down list boxes, spinners, and other controls. For example, if you have a logical field in a table, when you run the form, a user can tell which record values are true (.T.) and which record values are false (.F.) by seeing whether the check box is set. Changing the value is as easy as setting or clearing the check box.

You can add controls to grid columns interactively in the Form Designer or write code to add the controls to the columns at run time.

To interactively add controls to a grid column

  1. Add a grid to a form.

  2. In the Properties window, set the ColumnCount property of the grid to the number of desired columns.

    For example, type 2 for a two-column grid.

  3. In the Properties window, select the parent column for the control from the Object box.

    For example, select Column1 to add a control to Column1. The border of the grid changes to indicate that you are editing a contained object when you select the column.

  4. Select the desired control on the Form Controls toolbar and click in the parent column.

    The new control will not be displayed in the grid column in the Form Designer, but it will be visible at run time.

  5. In the Properties window, make sure the control is displayed indented under the parent column in the Object box.

    If the new control is a check box, set the Caption property of the check box to " " and the Sparse property of the column to false (.F.).

  6. Set the ControlSource property of the parent column to the desired table field.

    For example, the ControlSource of the column in the following illustration is products.discontinu from Testdata.dbc in the Visual FoxPro \Samples\Data directory.

  7. Set the CurrentControl property of the parent column to the new control.

When you run the form, the control is displayed in the grid column.

Tip   If you want to be able to center a check box in a grid column, create a container class, add a check box to the container class, and adjust the position of the check box in the container class. Add the container class to the grid column and set the ControlSource of the check box to the desired field.

To remove controls from grid columns in the Form Designer

  1. In the Object box of the Properties window, select the control.

  2. Activate the Form Designer.

    If the Properties window is visible, the control name is displayed in the Object box.

  3. Press the DELETE key.

You also can add controls to a grid column using the AddObject method in code.

To programmatically add controls to a grid column

For example, the following lines of code in the Init event of a grid add two controls to a grid column and specify one of them as the current control:

THIS.grcColumn1.AddObject("spnQuantity", "SPINNER")
THIS.grcColumn1.AddObject("cboQuantity", "COMBOBOX")
THIS.grcColumn1.CurrentControl = "spnQuantity"
* The following lines of code make sure the control is visible
* and is diplayed in every row in the grid
THIS.grcColumn1.spnQuantity.Visible = .T.
THIS.grcColumn1.Sparse = .F.

In this example, Column1 has three possible current control values:

  • spnQuantity

  • cboQuantity

  • Text1 (the default control)

    Note   Properties set on the Grid level are not passed on to the columns or headers. In the same way, you must set properties of the headers and contained controls directly; they do not inherit their properties from settings at the Column level.

    Tip   For the best display of combo boxes in grid columns, set the following combo box properties:

    BackStyle = 0      && Transparent
    Margin = 0
    SpecialEffect = 1 && Plain
    BorderStyle = 0      && None
    

Using Conditional Formatting in Grids

Special formatting in a grid can make it easier for a user to scan through the records in the grid and locate certain information. To provide conditional formatting, use the dynamic font and color properties of a column.

For example, you can add a grid to a form and set the ColumnCount property to 2. Set the ControlSource property of the first column to orders.to_name and the ControlSource property of the second column to orders.order_net. To display order totals less than 500.00 with a forecolor of black and order totals greater than or equal to 500.00 with a foreground color of red, include the following line in the grid's Init event code:

THIS.Column2.DynamicForeColor = ;
   "IIF(orders.order_net >= 500, RGB(255,0,0), RGB(0,0,0))"

Common Grid Properties

The following grid properties are commonly set at design time.

Property Description
ChildOrder The foreign key of the child table that is joined with the primary key of the parent table.
ColumnCount Number of columns. If ColumnCount is set to - 1, the grid has as many columns as there are fields in the grid's RecordSource.
LinkMaster The parent table for child records displayed in the grid.
RecordSource The data to be displayed in the grid.
RecordSourceType Where the data displayed in the grid comes from:
a table, an alias, a query, or a table selected by the user in response to a prompt.

Common Column Properties

The following column properties are commonly set at design time.

Property Description
ControlSource The data to be displayed in the column. This is often a field in a table.
Sparse If Sparse is set to true (.T.), controls in a grid are displayed as controls only when the cell in the column is selected. Other cells in the column display the underlying data value in a text box. Setting Sparse to true (.T.) allows faster repainting if a user is scrolling through a grid with a lot of displayed rows.
CurrentControl Which control in the grid is active. The default is Text1, but if you add a control to the column, you can specify it as the CurrentControl.

Note   The ReadOnly property of a control inside the column is overridden by the ReadOnly property of the Column. If you set the ReadOnly property of the control in a column in the code associated with the AfterRowColChange event, the new setting will be valid while you are in that cell.

See Also

Setting Up a One-to-Many Form Using the Grid Control | Making Controls Easier to Use | Using Controls | Extending Forms | Form Designer | Controls and Objects