Selecting Multiple Records in Microsoft DataGridView

Applies to TestComplete 15.64, last modified on May 16, 2024

When testing an application that uses DataGridView controls, you may need to select multiple rows, columns or cells in the grid. You can then perform various operations over the selected elements, for example, copy their data to the clipboard. Note that it is possible to select multiple elements in the DataGridView control only if the grid’s MultiSelect property is True. If this property is False, multiple selection is not possible.

The following sections describe approaches that can be used to select multiple elements in the DataGridView control:

To perform these actions, TestComplete should have access to internal objects, properties and methods of the DataGridView control. For this purpose, the .NET Application Support and Microsoft Control Support plugins must be installed and enabled.

When testing Microsoft DataGridView controls, use specific methods and properties of the corresponding MicrosoftDataGridView object. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with an object’s properties and methods from your scripts. However, when testing a DataGridView control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

Simulating Selections With Mouse

Normally, the 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 the cell (row, column) will add it to the current selection, and holding Shift will extend selection to the selected cell (row, column).

To simulate a click on a grid cell, row indicator or column header, you can use the ClickCell, ClickRowIndicator and ClickColumnHeader actions of the MicrosoftDataGridView object. The parameters of these actions specify the cell (row or column) to be clicked, and also whether the Shift, Ctrl, Alt or combination of these keys should be pressed during the click.

Below is an example that illustrates how you can use these actions to select multiple rows and a range of rows and cells in the grid.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid, RowIndexes;

  // Obtain the grid object
  p = Sys.Process ("DataGridViewSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1");

  // Select multiple rows
  RowIndexes = new Array (0, 2, 4, 6, 8);
  SelectRows (Grid, RowIndexes);

  // Select a range of rows
  SelectRowRange (Grid, 1, 5);

  // Select a range of cells
  SelectCellRange (Grid, 2, "Product", 7, "City");
}

// Selects the specified rows
function SelectRows (Grid, RowIndexes)
{
  // Select the first of the specified rows
  Grid.ClickRowIndicator (RowIndexes[0]);
  // Add other rows to the selection
  for (var i=1; i<RowIndexes.length; i++)
    Grid.ClickRowIndicator (RowIndexes[i], skCtrl);
}

// Selects a range of rows
function SelectRowRange (Grid, StartRowIndex, EndRowIndex)
{
  // Select the first row of the range
  Grid.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift);
}

// Selects a range of cells
function SelectCellRange (Grid, StartRowIndex, StartColumnId, EndRowIndex, EndColumnId)
{
  // Select the first cell of the range
  Grid.ClickCell (StartRowIndex, StartColumnId);
  // Extend the selection
  Grid.ClickCell (EndRowIndex, EndColumnId, skShift);
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process ("DataGridViewSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  # Select multiple rows
  RowIndexes = list(0, 2, 4, 6, 8)
  SelectRows (Grid, RowIndexes)

  # Select a range of rows
  SelectRowRange (Grid, 1, 5)

  # Select a range of cells
  SelectCellRange (Grid, 2, "Product", 7, "City")

# Selects the specified rows
def SelectRows (Grid, RowIndexes):
  # Select the first of the specified rows
  Grid.ClickRowIndicator (RowIndexes[0])
  # Add other rows to the selection
  for i in range(1, RowIndexes.length-1):
    Grid.ClickRowIndicator (RowIndexes[i], skCtrl) 

# Selects a range of rows
def SelectRowRange (Grid, StartRowIndex, EndRowIndex):
  # Select the first row of the range
  Grid.ClickRowIndicator (StartRowIndex)
  # Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift) 

# Selects a range of cells
def SelectCellRange (Grid, StartRowIndex, StartColumnId, EndRowIndex, EndColumnId):
  # Select the first cell of the range
  Grid.ClickCell (StartRowIndex, StartColumnId)
  # Extend the selection
  Grid.ClickCell (EndRowIndex, EndColumnId, skShift)

VBScript

Sub Main
  Dim p, Grid, RowIndexes

  ' Obtain the grid object
  Set p = Sys.Process ("DataGridViewSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  ' Select multiple rows
  RowIndexes = Array (0, 2, 4, 6, 8)
  Call SelectRows (Grid, RowIndexes)

  ' Select a range of rows
  Call SelectRowRange (Grid, 1, 5)

  ' Select a range of cells
  Call SelectCellRange (Grid, 2, "Product", 7, "City")
End Sub

' Selects the specified rows
Sub SelectRows (Grid, RowIndexes)
  ' Select the first of the specified rows
  Call Grid.ClickRowIndicator (RowIndexes(0))
  ' Add other rows to the selection
  For i=1 To UBound(RowIndexes)
    Call Grid.ClickRowIndicator (RowIndexes(i), skCtrl)
  Next
End Sub

' Selects a range of rows
Sub SelectRowRange (Grid, StartRowIndex, EndRowIndex)
  ' Select the first row of the range
  Call Grid.ClickRowIndicator (StartRowIndex)
  ' Extend the selection
  Call Grid.ClickRowIndicator (EndRowIndex, skShift)
End Sub

' Selects a range of cells
Sub SelectCellRange (Grid, StartRowIndex, StartColumnId, EndRowIndex, EndColumnId)
  ' Select the first cell of the range
  Call Grid.ClickCell (StartRowIndex, StartColumnId)
  ' Extend the selection
  Call Grid.ClickCell (EndRowIndex, EndColumnId, skShift)
End Sub

DelphiScript

procedure SelectRows (Grid, RowIndexes); forward;
procedure SelectRowRange (Grid, StartRowIndex, EndRowIndex); forward;
procedure SelectCellRange (Grid, StartRowIndex, StartColumnId, EndRowIndex, EndColumnId); forward;

procedure Main;
var p, Grid, RowIndexes : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('DataGridViewSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGridView1');

  // Select multiple rows
  RowIndexes := BuiltIn.CreateVariantArray(0, 4);
  RowIndexes[0] := 0; RowIndexes[1] := 2;
  RowIndexes[2] := 4; RowIndexes[3] := 6;
  RowIndexes[4] := 8;
  SelectRows (Grid, RowIndexes);

  // Select a range of rows
  SelectRowRange (Grid, 1, 5);

  // Select a range of cells
  SelectCellRange (Grid, 2, 'Product', 7, 'City');
end;

// Selects the specified rows
procedure SelectRows (Grid, RowIndexes);
var i : OleVariant;
begin
  // Select the first of the specified rows
  Grid.ClickRowIndicator (RowIndexes[0]);
  // Add other rows to the selection
  for i := 1 to VarArrayHighBound(RowIndexes, 1) do
    Grid.ClickRowIndicator (RowIndexes[i], skCtrl)
end;

// Selects a range of rows
procedure SelectRowRange (Grid, StartRowIndex, EndRowIndex);
begin
  // Select the first row of the range
  Grid.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift);
end;

// Selects a range of cells
procedure SelectCellRange (Grid, StartRowIndex, StartColumnId, EndRowIndex, EndColumnId);
begin
  // Select the first cell of the range
  Grid.ClickCell (StartRowIndex, StartColumnId);
  // Extend the selection
  Grid.ClickCell (EndRowIndex, EndColumnId, skShift);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, RowIndexes;

  // Obtain the grid object
  p = Sys["Process"]("DataGridViewSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGridView1");

  // Select multiple rows
  RowIndexes = new Array (0, 2, 4, 6, 8);
  SelectRows (Grid, RowIndexes);

  // Select a range of rows
  SelectRowRange (Grid, 1, 5);

  // Select a range of cells
  SelectCellRange (Grid, 2, "Product", 7, "City");
}

// Selects the specified rows
function SelectRows (Grid, RowIndexes)
{
  // Select the first of the specified rows
  Grid["ClickRowIndicator"](RowIndexes[0]);
  // Add other rows to the selection
  for (var i=1; i<RowIndexes["length"]; i++)
    Grid["ClickRowIndicator"](RowIndexes[i], skCtrl);
}

// Selects a range of rows
function SelectRowRange (Grid, StartRowIndex, EndRowIndex)
{
  // Select the first row of the range
  Grid["ClickRowIndicator"](StartRowIndex);
  // Extend the selection
  Grid["ClickRowIndicator"](EndRowIndex, skShift);
}

// Selects a range of cells
function SelectCellRange (Grid, StartRowIndex, StartColumnId, EndRowIndex, EndColumnId)
{
  // Select the first cell of the range
  Grid["ClickCell"](StartRowIndex, StartColumnId);
  // Extend the selection
  Grid["ClickCell"](EndRowIndex, EndColumnId, skShift);
}

Using Internal Methods of the DataGridView Control

The DataGridView control has special internal methods that let you select multiple rows, columns and cells in the grid. They are listed in the table below.

Method Description
SelectRowRange (RowIndexFrom, RowIndexTo, Select) Selects or unselects the range of cells specified by the top and bottom row.
SelectCellRange (ColIndexFrom, RowIndexFrom, ColIndexTo, RowIndexTo, Select) Selects or unselets a range of cells specified by the top-left and bottom-right cells.
SelectColumnRange (ColIndexFrom, ColIndexTo, Select) Selects or unselects the range of columns specified by the left and right column.
ClearSelection Unselects all selected rows, columns and cells in the grid.

The code snippet below demonstrates how you can use these methods in scripts:

JavaScript, JScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process ("DataGridViewSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1");

  // Select a range of rows
  Grid.ClearSelection();
  Grid.SelectRowRange (1, 5, true);

  aqUtils.Delay (1000);

  // Select a range of cells
  Grid.ClearSelection();
  Grid.SelectCellRange (2, 1, 6, 7, true);
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process ("DataGridViewSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  # Select a range of rows
  Grid.ClearSelection()
  Grid.SelectRowRange (1, 5, True)

  aqUtils.Delay (1000)

  # Select a range of cells
  Grid.ClearSelection()
  Grid.SelectCellRange (2, 1, 6, 7, True)

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process ("DataGridViewSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  ' Select a range of rows
  Grid.ClearSelection
  Call Grid.SelectRowRange (1, 5, True)

  Call aqUtils.Delay (1000)

  ' Select a range of cells
  Grid.ClearSelection
  Call Grid.SelectCellRange (2, 1, 6, 7, True)
End Sub

DelphiScript

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('DataGridViewSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGridView1');

  // Select a range of rows
  Grid.ClearSelection;
  Grid.SelectRowRange (1, 5, true);

  aqUtils.Delay (1000);

  // Select a range of cells
  Grid.ClearSelection;
  Grid.SelectCellRange (2, 1, 6, 7, true);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys["Process"]("DataGridViewSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGridView1");

  // Select a range of rows
  Grid["ClearSelection"]();
  Grid["SelectRowRange"](1, 5, true);

  aqUtils["Delay"] (1000);

  // Select a range of cells
  Grid["ClearSelection"]();
  Grid["SelectCellRange"](2, 1, 6, 7, true);
}

See Also

Working With Microsoft DataGridView
Selecting Cells in Microsoft DataGridView
Obtaining Selected Records in Microsoft DataGridView
ClickCell Action (Grid Controls)
ClickColumnHeader Action (Grid Controls)
ClickRowIndicator Action (Grid Controls)

Highlight search results