Selecting Multiple Rows in Developer Express XtraGrid
It is possible to select multiple rows in Developer Express XtraGrid control. You can then perform various operations over the selected rows, for example, copy their data to the clipboard, delete them, and so on. This article describes two approaches to performing multiple selections: simulating mouse selections and using XtraGrids internal methods. Both 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.
Note also, that the multiple selection in a particular grid view is only enabled if the views
OptionsSelection.MultiSelect is True. If this property if False, only one row can be selected in a view at a time.
Simulating Selections With Mouse
The users can select multiple rows in the XtraGrid control by pressing
Shift or both keys while selecting them. The following table lists possible variants:
|Action ||Description |
|Clicking a row holding no key. ||Clears the previous selection and selects the clicked row. |
|Clicking a row holding the Shift key. ||Clears the previous selection and selects the range of rows between the clicked row and the previously selected ones. |
|Clicking a row holding the Ctrl key. ||Selects the clicked row, preserving the previous selection. |
|Clicking a row holding the Ctrl and Shift keys. ||Selects the range of rows between the previously selected row and the clicked one, preserving the previous selection. |
You can simulate
Shift-clicks on grid rows using the
Click action applied to the grid control. This action takes the coordinates of the point to click, and the optional Shift parameter that specifies the key to be pressed when performing the click. To select several rows, use successive
Click calls with the desired row coordinates and the needed Shift parameter values.
An individual row can be selected by simulating a click on its indicator. The row indicators, however, are displayed only if the views
OptionsView.ShowIndicator property is True. If so, you can determine the row indicator coordinates using the following statement:
Here, viewObj is the grid view in which the row resides; for grids that do not work in the master detail mode it should be
.MainView. The RowIndex parameter specifies the zero-based index of the desired row. 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 to select multiple rows in the XtraGrid control by simulating
Shift-clicks on row indicators. It works with the GridTutorials sample application that is shipped with the Developer Express XtraGrid Suite. The example contains four routines:
Main is the main script routine. It obtains the scripting object corresponding to the XtraGrid control and calls
SelectRows to select multiple rows in the grid.
SelectRange - Selects the specified range of rows in the grid. It has the following parameters:
- Grid - Specifies the tested XtraGrid control.
- View - Specifies the grid view in which the selection should be performed. If the null value (
Nothing in VBScript) is passed in this parameter, the main (top-level) grid view is used.
- StartRowIndex and EndRowIndex - Specify indexes (zero-based) of the first and the last rows of the range to be selected.
SelectRows - Selects the grid rows specified by their indexes. The parameters of this routine are similar to those of the
SelectRange routine, except for the RowIndexes parameter that specifies an array containing indexes of rows to be selected.
ClickRowIndicator - A helper routine that simulates a click on a row indicator.
Using XtraGrids Internal Methods
The XtraGrid control has a number of internal methods that can be used for selecting or unselecting multiple rows. They are listed in the table below:
|Method ||Description |
|viewObj.SelectRow (RowIndex) ||Selects the row specified by its index, preserving the previous selection. |
|viewObj.SelectRange (StartRowIndex, EndRowIndex) ||Selects rows in the specified range, preserving the previous selection. |
|viewObj.SelectAll() ||Selects all rows within a view. |
|viewObj.UnselectRow (RowIndex) ||Unselects the specified row. |
|viewObj.ClearSelection() ||Unselects all selected rows in a view. |
The following example demonstrates how you can use these methods in scripts. It performs the same actions as the previous example, but the implementation of the
SelectRows routines is different.