Selecting Multiple Rows in Microsoft DataGrid

Applies to TestComplete 14.71, last modified on April 22, 2021

When testing an application that uses Microsoft DataGrid .NET controls, you may need to select multiple rows in the grid. You can then perform various operations over the selected rows, for example, obtain their data.

Normally, the users can select multiple rows in the DataGrid control by pressing Ctrl or Shift while selecting the rows. Holding Ctrl while selecting the row will add it to the current selection, and holding Shift will extend selection to the selected row.

To simulate a click on a grid row indicator, you can use the ClickRowIndicator action of the MicrosoftDataGrid object. Its parameters specify the row to be clicked, and also whether the Shift, Ctrl, Alt or combination of these keys should be pressed during the click.

Note: The ClickRowIndicator actions as well as other methods, properties and actions of the MicrosoftDataGrid object are only available if the Microsoft Control Support plugin is installed and enabled.

Below is an example that illustrates how you can use the ClickRowIndicator action to select multiple rows and a range of rows in the grid control.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid, RowIndexes;
  // Obtain the grid object
  p = Sys.Process("DataGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1");

  // Select multiple rows
  RowIndexes = new Array (0, 1, 4, 7);
  SelectRows (Grid, RowIndexes);

  // Select a range of rows
  SelectRange (Grid, 2, 6);
}

// 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 SelectRange (Grid, StartRowIndex, EndRowIndex)
{
  // Select the first row of the range
  Grid.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift);
}

Python

def Main ():
  # Obtain the grid object
  p = Sys.Process("DataGridSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  # Select multiple rows
  RowIndexes = list(0, 1, 4, 7)
  SelectRows (Grid, RowIndexes)

  # Select a range of rows
  SelectRange (Grid, 2, 6)

# 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 SelectRange (Grid, StartRowIndex, EndRowIndex):
  # Select the first row of the range
  Grid.ClickRowIndicator (StartRowIndex)
  # Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift)

VBScript

Sub Main
  Dim p, Grid, RowIndexes
  ' Obtain the grid object
  Set p = Sys.Process("DataGridSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  ' Select multiple rows
  RowIndexes = Array (0, 1, 4, 7)
  Call SelectRows (Grid, RowIndexes)

  ' Select a range of rows
  Call SelectRange (Grid, 2, 6)
End Sub

' Selects the specified rows
Sub SelectRows (Grid, RowIndexes)
  ' Select the first of the specified rows
  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 SelectRange (Grid, StartRowIndex, EndRowIndex)
  ' Select the first row of the range
  Grid.ClickRowIndicator (StartRowIndex)
  ' Extend the selection
  Call Grid.ClickRowIndicator (EndRowIndex, skShift)
End Sub

DelphiScript

procedure SelectRows (Grid, RowIndexes); forward;
procedure SelectRange (Grid, StartRowIndex, EndRowIndex); forward;

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

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

  // Select a range of rows
  SelectRange (Grid, 2, 6);
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 SelectRange (Grid, StartRowIndex, EndRowIndex);
begin
  // Select the first row of the range
  Grid.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, RowIndexes;
  // Obtain the grid object
  p = Sys["Process"]("DataGridSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGrid1");

  // Select multiple rows
  RowIndexes = new Array (0, 1, 4, 7);
  SelectRows (Grid, RowIndexes);

  // Select a range of rows
  SelectRange (Grid, 2, 6);
}

// 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 SelectRange (Grid, StartRowIndex, EndRowIndex)
{
  // Select the first row of the range
  Grid["ClickRowIndicator"](StartRowIndex);
  // Extend the selection
  Grid["ClickRowIndicator"](EndRowIndex, skShift);
}

See Also

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

Highlight search results