Partager via


How to: Display 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 (Visual FoxPro), 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 (Visual FoxPro), 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
    

See Also

Tasks

How to: Set Up a One-to-Many Form Using the Grid Control

How to: Make Controls Easier to Use

Concepts

Formatting Grids

Controls and Objects Created in Earlier Versions

Reference

Controls for Manipulating Rows of Data

Controls for Extending Forms

Form Designer

Other Resources

Using Controls