Working With TDBGrid - Selecting Cells
Test and Monitor | Posted March 29, 2007

One of the actions that you will often perform over Borland TDBGrid controls is selecting cells. This article explains how you can select TDBGrid control's cells from scripts:

General Notes

Using the TDBGrid.Col Property

Simulating Keyboard Shortcuts

General Notes

The cell selection is only possible if the grid does not work in row selection mode. The row selection mode is active, if the grid's Options property contains the dgRowSelect value (this is an internal property of the TDBGrid control). If you are not sure in which mode the tested grid functions, you may ask the tested application's developer. Also, you can determine the mode visually by watching the grid's behavior when you are navigating through the records. If the grid highlights whole rows rather than individual cells, then the row selection mode is active and you cannot simulate the cell selection.

Before selecting a cell, you need to locate the desired record within the grid. For example, you can search for and activate the desired row using methods described the Working With TDBGrid - Searching for Records article. After you have determined the cell position in the grid, you can select the cell in one of the following ways:

  • By focusing the row and column in which the desired cell resides using internal properties and methods of the TDBGrid control.
  • By simulating keyboard shortcuts (for example, Arrow keys).
  • By simulating a mouse click on the cell. In this case, the the cell is automatically put into edit mode so you can modify its value.

All of these approaches work equally well. However, in case of using the Col property, TestComplete does not activate the form where the grid resides (if you decide to simulate keystrokes or a click, the grid's form will be activated). You can use the approach that better suites your needs.

Using the TDBGrid.Col Property

The Col property is only available if the tested application is compiled as an Open Application with debug information. The property is exposed by the Debug Info Agent™. It specifies the currently focused cell in the active row of the grid.

Below is a code snippet that demonstrates how you can select a cell using the Col property of the TDBGrid control. The sample code contains the following script routines:

  • The Main routine obtains the scripting object that corresponds to the grid and then calls the SelectCell routine.
  • The ActivateRow procedure is a helper procedure that is used to select the desired row. Its two parameters - GridObject and RowIndex - specify the grid object and the index of the desired row.
  • SelectCell simulates the cell selection. It selects the cell's row and then assigns the index of the desired column to the Col property. The routine has the following parameters:
    • GridObject -- specifies the scripting object that corresponds to the TDBGrid control,
    • RowIndex and ColIndex -- specify the row and column indexes of the cell.

    Note that we assume that the cell array is zero-based. That is, the index of the leftmost cell containing data, is 0. However, the grid may contain an indicator (this happens if the grid's Options property contains the dgIndicator value). If the indicator is visible, it becomes the leftmost column, and in grid terms, the first data cell gets the index 1.

    To determine whether the indicator is visible or not, we use the IndicatorOffset property of the TDBGrid control. This is a native property of the grid. It specifies the index of the first data cell in a row. If the indicator is visible, the property contains 1. If the indicator is hidden, the property contains 0.

    When assigning a value to the Col property, we increase the specified column index by the value of the IndicatorOffset property.

Simulating Keyboard Shortcuts

You can select a grid cell by simulating keyboard shortcuts over the grid control. For instance, you can simulate the Home shortcut to move the focus to the first cell in a row, and then simulate a number of Right Arrow key presses to move the selection to the desired cell. The code below demonstrates how you can select a cell by simulating shortcuts. The sample code contains three routines:

Close

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox