The XtraGrid control supports hierarchical data representation. If the grid displays data of multiple nested data tables, then each data row can have child rows containing data associated with this row. To view the child data, the user need to expand its parent row. The grid data can also be grouped by one or several column, so that individual rows are organized into groups. This article explains several approaches that you can use to expand and collapse XtraGrid rows and groups from your TestComplete scripts:
Note: All these approaches require access to internal properties, methods and objects of the XtraGrid control. To enable TestComplete to access them, you need to install and enable the .NET Open Application Support plug-in.
Simulating Clicks on Expand Buttons
The group and master rows in the XtraGrid control have expand buttons that are used to expand or collapse these rows. When a row is collapsed, its expand button displays a + sign, and when it is expanded, the button displays a - sign.
To simulate a click on a row expand button, use the Click
action added to ann onscreen objects by TestComplete. It requires you to know the coordinates of the expand button relative to the grid control. It is possible to determine them via the grid views internal properties and methods.
The following statement lets you determine the coordinates of the expand button within a particular row:
viewObj.ViewInfo.RowsInfo.GetInfoByHandle(RowIndex).Cells.Item(CellIndex).CellButtonRect
Here, viewObj is the object corresponding to the grid view and RowIndex specifies the zero-based index of the desired row. The CellIndex parameter specifies the index of the row cell containing the expand button. It can be 1 or 0, depending on whether the row indicators are visible or not. To check if the row indicators are displayed, you can use the grid views IsShowRowIndicator
property.
To get the coordinates of a groups expand button, use the following statement:
viewObj
.ViewInfo.RowsInfo.GetInfoByHandle(GroupIndex).ButtonBounds
In this statement, GroupIndex is the internal -1-based index of the desired group. That is, index of the first group is -1, the second - -2 and so on.
The following example demonstrates how you can calculate the expand button coordinates and simulate clicks on them. This example works with the GridMainDemo sample application that is shipped with the Developer Express XtraGrid Suite. The example contains the following routines:
Main
- The main routine. It obtains the XtraGrid control and then calls ClickExpandButton
to simulate clicks on expand buttons of some grid rows and groups. ClickExpandButton
- Simulates a click on the expand button of the specified row or group. The routine has the following parameters:
- Grid - The tested XtraGrid control.
- View - The grids view to which the row or group belongs. If this parameter has the null value (
Nothing
in VBScript), it means that the row (group) resides in the grids main (top-level) view. - RowIndex - The index of the desired row or group. Data row indexes are zero-based and increase down the grid. That is, the first data row has index 0, the second - 1 and so on. Groups indexes are negative; they start from -1 and decrease down the grid. So, index of the first group is -1, the second - -2 and so on.
GetChildView
- A helper function that returns the currently visible child view for the specified row.
Simulating Keyboard Shortcuts
It is possible to expand and collapse XtraGrid rows and groups using the special keyboard shortcuts. For example, the Ctrl+Num Plus shortcut expands the currently selected group or master row, and Ctrl+Num Minus collapses it. To send these keystrokes to the grid control, use the Keys
action common to all onscreen objects in TestComplete.
Below is an example that demonstrates how to expand and collapse XtraGrid rows and groups by simulating keyboard shortcuts. It performs the same actions as the previous script, but uses the different approach. Besides the Main
routine, the script contains the helper SelectRow
routine that selects the specified row or group. This example also uses the helper GetChildView
routine, whose code you can find in the previous example.
Using Internal Methods of the XtraGrid Control
One more way to expand and collapse rows and groups in the XtraGrid control is to use its internal methods. The table below lists some methods that are used to expand and collapse XtraGrid rows and groups. All these methods apply to the view object that contains the desired row or group.
Method | Description |
ViewObj.ExpandMasterRow (RowIndex) | Expand a data row specified by its index (0-based). |
ViewObj.CollapseMasterRow (RowIndex) | Collapses a data row specified by its index (0-based). |
ViewObj.GetMasterRowExpanded (RowIndex) | Return True if the specified data row is expanded; otherwise False. |
ViewObj.ExpandGroupRow (GroupIndex) | Expands a group specified by its index (-1-based). |
ViewObj.CollapseGroupRow (GroupIndex) | Collapses a group specified by its internal index (-1-based). |
ViewObj.GetRowExpanded (GroupIndex) | Returns True if the specified group is expanded, and False otherwise. |
The following example demonstrates how you can use these methods to expand rows and groups in the XtraGrid control. This example performs the same actions as the previous examples, though it implements the different approach. The example contains the following routines:
Main
- The main routine. It obtains the XtraGrid control and then collapses and expands some rows and groups in the grid. Expand
and Collapse
- Expand or collapse the specified row or group, respectively. Both routines have the same parameters:
- Grid - The tested XtraGrid control.
- View - The grids view to which the row or group belongs. If this parameter has the null value (
Nothing
in VBScript), it means that the row (group) resides in the grids main (top-level) view. - RowIndex - The index of the desired row (0-based) or group (-1-based).
Both routines call the helper SetExpanded
routine to change the expanded state of the specified row or group. SetExpanded
is a helper routine that sets the expanded state of the specified row or group. The needed row (group) state is specified by the Expanded parameter: if it is True, the row (group) will be expanded, otherwise collapsed.
The example also uses a helper GetChildView
routine, but its code is not included in the given script. You can find the routine code in the example above.