Selecting Cells in Developer Express XtraGrid

  May 11, 2007

One of the actions that you will perform most often on a grid is
selecting cells. This article describes various approaches that can be
used to select a particular cell in the Developer Express XtraGrid
control:

  • Simulating clicks on cells.
  • Simulating keyboard shortcuts.
  • Using internal members and properties of the XtraGrid control.

Two notes before we proceed:

All these approaches assume that TestComplete has access to the internal methods, properties and objects of the XtraGrid control. That is, the .NET Open Application Support plug-in must be installed and enabled.

If the tested grid control contains a large number of records, you may need to locate the row (card) in which the desired cell resides. For example, you can search for the row (card) by the value or text in a particular column, as I have described in the previous article - Searching for Records in Developer Express XtraGrid.

Simulating Clicks on Cells

You can select a particular cell in the XtraGrid control by simulating a mouse click on it. For this purpose you can use the Click action applied to the grid control. This require you to know the grid-relative coordinates of the desired cell, which you can determine using internal properties and methods of the view object that contains this cell:

viewObj.ViewInfo.RowsInfo.GetInfoByHandle(RowHandle).Cells.Item_2(Column).Bounds

-- or --

viewObj.ViewInfo.Cards.CardFieldInfoBy(RowHandle, Column).Bounds

The first statement is uses in grids, banded and advanced banded vies, the second one - in card views. In these statements, view is a view object, RowHandle is a zero-based index of the row (card) that contains the cell, and Column is the object corresponding to the cell’s column (card field). The returned object is a .NET System.Drawing.Rectangle object that contains grid-relative coordinates of the specified cell.

Below is an example that illustrates how you can calculate the cell coordinates and simulate a cell click. This example works with the GridTutorials sample application that comes with the Developer Express XtraGrid Suite. The script contains the following routines:

  • Main is the main script routine. It uses the ClickCell routine to simulate clicks on cells in the XtraGrid controls with grid and card layouts.
  • ClickCell simulates a click on the grid cell. Before clicking the cell, it scrolls the view to make the cell visible, if needed. The routine has the following parameters:
    • Grid - Specifies the tested XtraGrid control.
    • View - The view in which the cell resides. If the null value (Nothing in VBScript) is passed in the View parameter, it is assumed that the cell is situatted in the grid’s main view.
    • RowHandle - The zero-based index of the row (card) where the cell is located.
    • ColumnId - The caption of the column that contains the cell, or the column’s zero-based position within the view.
  • GetColumn is a helper routine that returns the column object by its caption or by its position within the view.

Simulating Keyboard Shortcuts

The XtraGrid control supports various keyboard shortcuts that can be used to navigate through the grid. For example, pressing the Down Arrow key selects the next row, and Ctrl+Home navigates to the grid’s upper-left cell. For more information on what shortcuts can be used in the XtraGrid control, see the XtraGrid Suite documentation.

To simulate a keyboard shortcut, use the Keys action added to all onscreen objects by TestComplete. The following code snippet demonstrates how to simulate the Ctrl+Home shortcut in order to select the grid’s upper left cell:

Using XtraGrid Internal Members

The XtraGrid control has a number of internal properties and methods that can be used to select a particular cell. They are listed in the following table:

Property (Method) Description
FocusedRowHandle Property. Gets or sets the handle of the currently selected row (card).
FocusedColumn Property. Gets or sets the currently focused column (card field).
MoveFirst() Method. Selects the first visible row (card) within the view.
MoveNext() Method. Selects the row (card) following the currently focused one.
MovePrev() Method. Selects the row (card) preceding the currently focused one.
MoveLast() Method. Selects the last row (card) within the view.
MoveLastVisible() Method. Selects the last visible row (card) within the view.
MoveLastVisible(delta) Method. Moves the row (card) focus by the specified number of rows (cards).

Below is an example that demonstrates how you can select a cell in the XtraGrid control using the FocusedRowHandle and FocusedColumn properties.

This example works with the same GridTutorials sample application and contains the following routines:

  • Main is the main script routine. It obtains the scripting object corresponding to the XtraGrid control, calls SelectCell to select some grid cells and simulates the user input into the cells.
  • SelectCell - Selects the specified grid cell. The routine has the following parameters:
    • Grid - Specifies the tested XtraGrid control.
    • View - The view in which the cell resides. If the null value (Nothing in VBScript) is passed in the View parameter, it is assumed that the cell is situatted in the grid’s main view.
    • RowHandle - The zero-based index of the row where the cell is located.
    • ColumnId - The caption of the column that contains the cell or the column’s zero-based position within the view.
  • GetColumn - A helper function that returns a column object by the column caption or its zero-based position within the view.