Selecting Multiple Records in Microsoft DataGridView
When testing an application with the DataGridView control, you may need to select multiple records in the grid. For example, you may want to copy the selected records to the clipboard or perform other actions over the selection.
Normally, users can select multiple cells, rows and columns in the DataGridView control by pressing Ctrl or Shift while selecting the cells (rows, columns). Holding Ctrl while selecting a cell (row, column) will add it to the current selection, and holding Shift will extend the selection to the selected cell (row, column). Note that it is possible to select multiple elements in the DataGridView control only if the grids
MultiSelect property is True. If this property is False, multiple selection is impossible.
The way you select the entire DataGridView row or column depends on the value of the grids
|SelectionMode Value ||Description |
|CellSelect ||Clicking a cell selects that cell. It is impossible to select the entire row or column by clicking the header. |
|ColumnHeaderSelect ||Clicking the column header selects the entire column |
|FullColumnSelect ||Clicking the column cell, including the column header, selects the entire column. |
|FullRowSelect ||Clicking the row cell, including the row header, selects the entire row. |
|RowHeaderSelect ||Clicking the row header selects the entire row. |
You can simulate a click over a grid cell using the
Click action (I described how to do this in Selecting Cells in Microsoft DataGridView). To simulate Ctrl- and Shift-clicks over cells, you should specify an appropriate value in the Shift parameter of the
This approach assumes that TestComplete presses Shift or Ctrl each time it clicks an item. However, a more natural way of selecting multiple items is pressing the Shift or Ctrl key first, then clicking the needed records and finally releasing Shift or Ctrl. This approach lets you also use mouse selection in combination with keyboard selection. To implement this approach, you can use the special
Sys.Desktop.KeyUp methods that lets you hold and release the key with the specified virtual code.
Below is an example that illustrates this approach. It selects a range of rows specified by the top and bottom rows and a range of cells specified by the top-left and bottom-right cells. This example contains four routines:
Main is the main routine.
SelectRowsRange selects the grid rows in the RowStart..RowEnd range using Shift-clicks on row headers.
SelectCellsRange seelects the range of rows in the RowStart..RowEnd rows and ColStart..ColEnd columns. To simulate a cell click, it uses the
ClickCell routine whose code you can find in Selecting Cells in Microsoft DataGridView.
ClickRowHeader is a helper routine that performs the row header click. It is very similar to the