Common Operations for Grids

Applies to TestComplete 15.69, last modified on November 13, 2024

This topic describes general approaches that can be used to perform particular operations with the grid control. Most explanations in this topic assume that the grid control belongs to an Open Application, so that its internal properties, fields and methods are visible to TestComplete. To learn which properties and methods of the grid controls you can use to implement the desired operations in your scripts, please refer to the documentation on the grid control, or ask the tested application’s developers.

If your application is a non-Open (ordinary) application, it is still possible to test the grid control as a black-box object, that is by simulating mouse clicks and keyboard input over it, executing low-level procedures, and so on. See Working With Grids.

Searching for Records

You may need to find a certain record to make sure that data has been loaded into the grid correctly or to determine the position of the desired record within the grid. You can search for a record within the grid’s dataset, or directly within the grid. A general approach implies that you iterate through the grid rows in a loop and check if the current record matches the search criteria. Note that some grid controls may be configured so that they display cell values using a special formatting. In this case, the actual cell value differs from what you see on the screen. It is recommended that you explore the grid object in the tested application to learn the types of data displayed by the grid.

Some grid controls support the incremental search feature. In this case, you need to select any cell in a column in which you want to search and use the Sys.Keys method or the Keys action to “type” the sought-for value into the grid. As a result, the first cell in the focused column that matches the inputted string will become selected in the grid.

The grid control may also have built-in methods that can be used to search for the desired record within the grid.

Selecting Cells

It is possible to select a particular grid cell in the following ways:

  • By clicking the cell. To simulate a cell click, use the Click action applied to the grid control. It requires you to know the desired cell coordinates within the grid. Usually, it is possible to calculate them using internal properties and methods of the grid control.

  • Using the keyboard shortcuts. You can navigate to the desired cell using the keyboard shortcuts that change the row the column focus (for example, arrow keys). To simulate the keystrokes, use the Sys.Keys method or the Keys action applied to the grid control.

  • Using internal properties and methods of the grid control. Most grid controls have properties and methods that let you change the row, column and cell focus programmatically.

Activating In-place Editors

Before simulating the user input in a particular grid cell, you need to select it and activate its in-place editor. Some grid controls activate the cell’s in-place editor when the user clicks the cell, others may require the user to click the cell twice, or press F2, Enter or some other key. In general, you can use the Click action (or two successive Click calls) to simulate the click on the grid cell, or the Sys.Keys method to “press” the edit shortcuts.

The grid control may also provide built-in methods for activating the cell’s in-place editors.

Obtaining and Setting Cell Values

You can get and set data of a grid control that belongs to an Open Application in two ways:

  • By using the grid’s dataset. This approach is free from the grid’s user interface limitations, and thus, it is more universal for accessing grid data. However, if you need to test the functionality of the grid’s user interface, you should use direct access to grid cells.

  • By working with grid cells directly. This approach can be used to access the grid data as well as to test the functionality of the grid’s user interface. It assumes simulating mouse clicks, keystrokes and other user actions over individual grid cells.

Most grids support both ways of accessing their cells, yet, some grid controls may only support one of these approaches.

To find properties and methods needed to access grid data, explore your grid object in the Object Browser: select it in the object tree and look at the Object Properties tabbed page. If you decided to use the grid’s dataset for obtaining and changing the grid data, you should find the property that refers to the data source of your grid (this property may have the DataSource name). If it is there, click the ellipsis button for this property. You will see the properties and methods of the object returned by the DataSource property. Digging further, you will be able to determine the exact “path” to the table object containing the data and to refer to individual cells. The syntax that you can use in scripts will be displayed at the top-right portion of the Object Browser panel. Similarly, you can find methods and properties that provide access to columns, rows and cells of the grid control itself (the properties may have the Columns, Rows or Value names). To simplify the search process, you can refer to the documentation on the grid control or ask your developers for the objects, methods and properties that are needed to obtain cell values.

Note that the grid control may display data using a special format. For example, it may display the 34.6 value as $34.60, 0.05 as 5% and 3 as 3 order(s). Usually, grid controls provide properties that let you get the cell’s value itself as well as the text displayed in the cell. Remember this when creating your test scripts.

To modify the cell value, you can use the internal properties and methods of the grid control or simulate the corresponding user actions over it. Note that before inputting the new value, you need to select the needed grid cell and activate its in-place editor (see sections above). When the cell is in the edit mode, you can use the Sys.Keys method or the Keys action to “enter” a new value.

If the cell uses a specific in-place editor (for example, a combo box, a check box, a radio group), you can change the cell value using internal properties and methods of the editor object. If TestComplete recognizes the editor control as a Win32 object (you can check this by capturing the editor control with the target glyph), it extends the test object corresponding to the control with a number of properties, methods and actions that can be used to work with this control. In this case, you can use these extended members to simulate the user actions over the editor control.

Copying and Pasting Cell Values

Instead of inputting several equal cell values, it may be useful to copy the value of one cell to the clipboard, and paste values of other cells from the clipboard. Usually, you can copy and paste cell values using the Ctrl+C and Ctrl+V shortcuts. In this case, copying and pasting will require the following: selecting the desired grid cell, activating its in-place editor, selecting the cell contents to be copied or replaced (for example, using the Ctrl+A shortcut) and using the Sys.Keys method or the Keys action to simulate the Ctrl+C or Ctrl+V keystrokes.

You can also perform copying and pasting operations using the grid’s context menu, if it is present. To call the menu, you can either simulate the Application key with the Sys.Keys method or the Keys action, or right-click the grid cell using the ClickR action. To select the desired context menu command (Copy or Paste), use the Window.PopupMenu.Select method, or simulate the C or P keys (these are the hot keys for the Copy or Paste commands).

Another approach that can be used to copy or paste cell values is to use the Sys.Clipboard property to work with the clipboard programmatically. For example, you can obtain the cell value using one of the methods described in the previous section, and then assign this value to the Sys.Clipboard property. And vice versa, to paste the clipboard contents to the cell, you can assign the cell value with the value of the Sys.Clipboard property.

Selecting Multiple Rows

Some grid controls let the user select multiple rows in order for the application to perform certain operations with the selected data. Multiple rows can be selected by clicking grid rows with the Ctrl or Shift keys pressed. Usually, Ctrl-click on a row adds this row to the selection, and Shift-click on a row extends the selection to this row. To simulate Ctrl- and Shift-clicks on grid rows, use the Click action.

Usually, grid controls also contain special properties and methods that can be used to select multiple rows programmatically as well as obtain the selected rows data.

Iterating Through Rows

In your tests, you may need to perform operations that require executing the same script statements for each grid row. For example, you may need to export the grid data to a file or change multiple rows' data in a particular way. This can be done by iterating through rows in a loop. The loop counter should change from the index of the first grid row (which is usually 0) to the index of the last row (which is usually equal to the number of rows minus one). The loop body should include operations that process data in the current grid row.

Sorting Grid Data

In most grids that support sorting, this operation can be performed by clicking on a header of a grid column. The subsequent click switches the sort order (from ascending to descending, and vice versa). You can simulate column header clicks using the Click action applied to the grid control.

Grid controls may also have special methods that can be used to sort the data programmatically.

See Also

Working With Grids
Object-Specific Tasks
Simulating User Actions

Highlight search results