Working With TDBGrid - Activating In-place Editors
Test and Monitor | Posted March 29, 2007

The Borland TDBGrid control lets users change data directly within
grid cells. To do this, you have to activate a cell's in-place editor.
This article explains how to do this with TestComplete scripts.

General Notes

Simulating Cell Click

Simulating Keyboard Shortcuts

Note that to activate the in-place editor, TestComplete should have access to the internal methods and properties of the TDBGrid control. That is, the tested application must be compiled as an Open Application with debug information.

General Notes

Before activating a cell's in-place editor, you need to locate the desired cell within the grid and select it. That is, the test script should perform the following actions:

  • Search for the desired row.
  • Select the cell in this row.
  • Activate the cell's in-place editor.

You can activate the in-place editor either by sending the ENTER keystroke to the grid's window or by simulating a mouse click over the cell. Both these approaches are demonstrated below. For information on how to locate the desired row and select the cell, see the Working With TDBGrid - Selecting Records and Working With TDBGrid - Selecting Cells articles.

Simulating Cell Clicks

It is possible to put the cell into the edit mode by clicking it. You can use the Click action applied to the grid control for this purpose. This requires you to know the cell's coordinates within the grid. Calculating them may not be easy, since the grid contents can be scrolled both horizontally and vertically, grid columns have different widths, grid rows may have different heights and so on.

The following code demonstrates how to calculate the cell coordinates and simulate the click within the cell. The sample code contains three routines:

  • Main - The “main” routine of the sample. It obtains the scripting object that corresponds to the grid and then calls the ActivateEditorByClick routine.
  • ActivateRow - A helper procedure that is used to select the desired row.
  • ActivateEditorByClick simulates the click over the cell. It 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.

    The routine's code performs the following actions:

    • Selects the row, which contains the desired cell.
    • Calculates the coordinates of the cell's top-left corner within the grid window.
    • Calls the Click action of the grid window to simulates the mouse click at the specified coordinates.

    The vertical coordinate is stored in the Y variable. To find the vertical coordinate, we use the RowHeights property of the TDBGrid control. This is an iternal property that specifies the row heights in pixels. We go through all visible rows until we find the desired row and use the RowHeights(Index) property to obtain the height of each visible row. The Index parameter specifies the index of the desired visible row in the gird (row heights can differ from each other). The header's have and index of 0, that is, the header's height is specified by RowHeights(0). Since we only process visible rows, we ignore the position of the vertical scroll bar.

    The horizontal coordinate for the click is specified by the X variable. Then we iterate through all cells in a row and add their widths. If the indicator is visible, we add its width to the resultant X value.

    Note that the grid may not be wide enough to display all cells and the desired cell may reside out of the visible area. If so, we need to scroll the grid horizontally to make the cell visible. To do this, we simulate pressing the HOME key to move the focus to the leftmost cell and then we simulate the RIGHT ARROW keystroke over the grid window for each column that we process. These actions may cause the horizontal scrolling of the grid's contents. So, to obtain the horizontal coordinate of the desired cell, we decrease the X variable by the value of the GridObject.HScroll.Pos property (this property is provided by TestComplete. It specifies the current position of the horizontal scroll bar).

    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 index 1.

    To determine whether the indicator is visible or not, we use the IndicatorOffset property of the TDBGrid control. This is a internal 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 simulating a click we add 2 to the resultant X and Y values to ensure that the click is performed within the cell rather than on the cell's border.

    Simulating Keyboard Shortcuts

    The focused cell's in-place editor can activated upon pressing the ENTER key. You can simulate this key press using the Keys action applied to the TDBGrid control. The following code demonstrates how you can do this:


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