Selecting Multiple Rows and Cards in Developer Express XtraGrid

Applies to TestComplete 15.63, last modified on April 23, 2024

It is possible to select multiple rows (cards) in the XtraGrid control at one time. You can perform various operations over the selected rows (cards), for example, copy their data to the clipboard, delete them, and so on. Note, that the multiple selection in a particular grid view is only enabled if the view’s OptionsSelection.MultiSelect is True. If this property if False, only one row (card) can be selected in a view at a time.

The following sections describe approaches that can be used to select multiple rows (cards) in the XtraGrid control:

To perform these actions, TestComplete should have access to internal objects, properties and methods of the XtraGrid control. For this purpose, the .NET Application Support and Developer Express Control Support plugins must be installed and enabled. The latter lets you work with the XtraGrid control using methods and properties of the DevExpressXtraGrid object. Without this plugin, you will not be able to work with XtraGrid controls using their internal methods and properties.

When testing Developer Express XtraGrid controls, use specific methods and properties of the corresponding DevExpressXtraGrid 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 an XtraGrid 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

The users can select multiple rows (cards) in the XtraGrid control by pressing Ctrl or Shift or both keys while selecting them. The following table lists possible variants:

Action Description
Clicking a row (card) holding no key. Clears the previous selection and selects the clicked row (card).
Clicking a row (card) holding the Shift key. Clears the previous selection and selects the range of rows (cards) between the clicked row (card) and the previously selected ones.
Clicking a row (card) holding the Ctrl key. Selects the clicked row (card), preserving the previous selection.
Clicking a row (card) holding the Ctrl and Shift keys. Selects the range of rows (cards) between the previously selected row (card) and the clicked one, preserving the previous selection.

You can simulate Ctrl- and Shift-clicks on grid rows (cards) using the ClickRowIndicator action of the DevExpressXtraGrid or DevExpressXtraGridView objects. The action of a DevExpressXtraGrid object affects rows of the main data view, while the action of a DevExpressXtraGridView relates to rows of a corresponding child data view. The parameters of these action specify the cell to be clicked, and also whether the Shift, Ctrl, Alt or a combination of these keys should be pressed during the click. To select several rows (cards), you should use successive ClickRowIndicator calls with the needed Shift parameter values.

Below is an example that illustrates how you can select multiple rows in the XtraGrid control.

Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript, JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Select a range of rows
  SelectRange (Grid, 4, 8);

  aqUtils.Delay (1000);

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

// Selects the specified range of rows
function SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex)
{
  // Select the first row of the range
  ActiveViewObj.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  ActiveViewObj.ClickRowIndicator (EndRowIndex, skShift);
}

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

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridTutorials")
  frmMain = p.WinFormsObject("frmMain")
  # Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Select a range of rows
  SelectRange (Grid, 4, 8)

  aqUtils.Delay (1000)

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

# Selects the specified range of rows
def SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex):
  # Select the first row of the range
  ActiveViewObj.ClickRowIndicator (StartRowIndex)
  # Extend the selection
  ActiveViewObj.ClickRowIndicator (EndRowIndex, skShift)

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

VBScript

Sub Main
  Dim p, frmMain, Grid, RowIndexes

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridTutorials")
  Set frmMain = p.WinFormsObject("frmMain")
  ' Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' Select a range of rows
  Call SelectRange (Grid, 4, 8)

  aqUtils.Delay 1000

  ' Select multiple rows
  RowIndexes = Array (1, 2, 4, 7, 11)
  Call SelectRows (Grid, RowIndexes)
End Sub

' Selects the specified range of rows
Sub SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex)
  ' Select the first row of the range
  Call ActiveViewObj.ClickRowIndicator (StartRowIndex)
  ' Extend the selection
  Call ActiveViewObj.ClickRowIndicator (EndRowIndex, skShift)
End Sub

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

DelphiScript

procedure SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex : OleVariant); forward;
procedure SelectRows (ActiveViewObj, RowIndexes : OleVariant); forward;

procedure Main;
var p, frmMain, Grid, RowIndexes : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridTutorials');
  frmMain := p.WinFormsObject('frmMain');
  // Select the 'Multi Select' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Multi Select';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Select a range of rows
  SelectRange (Grid, 4, 8);

  aqUtils.Delay (1000);

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

// Selects the specified range of rows
procedure SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex : OleVariant);
begin
  // Select the first row of the range
  ActiveViewObj.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  ActiveViewObj.ClickRowIndicator (EndRowIndex, skShift);
end;

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

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridTutorials");
  frmMain = p["WinFormsObject"]("frmMain");
  // Select the "Multi Select" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Multi Select";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Select a range of rows
  SelectRange (Grid, 4, 8);

  aqUtils["Delay"] (1000);

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

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

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

Using XtraGrid Internal Methods

The XtraGrid control has a number of internal methods that can be used for selecting or unselecting multiple rows (cards). They are listed in the table below:

Method Description
SelectRow (RowIndex) Selects the row (card) specified by its index, preserving the previous selection.
SelectRange (StartRowIndexEndRowIndex) Selects rows (cards) in the specified range, preserving the previous selection.
SelectAll() Selects all rows (cards) within a view.
UnselectRow (RowIndex) Unselects the specified row (card).
ClearSelection() Unselects all selected rows (cards) in a view.
Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Select a range of rows
  SelectRange_2 (Grid, null, 8, 14);

  aqUtils.Delay (1000);

  // Select several rows
  RowIndexes = new Array (1, 2, 4, 7, 11);
  SelectRows_2 (Grid, null, RowIndexes);
}

function SelectRows_2 (Grid, View, RowIndexes)
{
  // Get the grid view object, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;

  // Clear the previous selection
  View.ClearSelection();

  // Select the specified rows
  for (let i=0; i<RowIndexes.length; i++)
    View.SelectRow (RowIndexes[i]);
}

function SelectRange_2 (Grid, View, StartRowIndex, EndRowIndex)
{
  // Get the grid view object, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;

  // Clear the previous selection
  View.ClearSelection();

  // Select the specified range of rows
  View.SelectRange (StartRowIndex, EndRowIndex);
}

JScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Select a range of rows
  SelectRange_2 (Grid, null, 8, 14);

  aqUtils.Delay (1000);

  // Select several rows
  RowIndexes = new Array (1, 2, 4, 7, 11);
  SelectRows_2 (Grid, null, RowIndexes);
}

function SelectRows_2 (Grid, View, RowIndexes)
{
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid.MainView;

  // Clear the previous selection
  View.ClearSelection();

  // Select the specified rows
  for (var i=0; i<RowIndexes.length; i++)
    View.SelectRow (RowIndexes[i]);
}

function SelectRange_2 (Grid, View, StartRowIndex, EndRowIndex)
{
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid.MainView;

  // Clear the previous selection
  View.ClearSelection();

  // Select the specified range of rows
  View.SelectRange (StartRowIndex, EndRowIndex);
}

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridTutorials")
  frmMain = p.WinFormsObject("frmMain")
  # Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Select a range of rows
  SelectRange_2 (Grid, None, 8, 14)

  aqUtils.Delay (1000)

  # Select several rows
  RowIndexes = list(1, 2, 4, 7, 11)
  SelectRows_2 (Grid, None, RowIndexes)

def SelectRows_2 (Grid, View, RowIndexes):
  # Get the grid view object, if it is not specified
  if (View == None):
    View = Grid.MainView

  # Clear the previous selection
  View.ClearSelection()

  # Select the specified rows
  for i in range(0, RowIndexes.length-1):
    View.SelectRow (RowIndexes[i])

def SelectRange_2 (Grid, View, StartRowIndex, EndRowIndex):
  # Get the grid view object, if it is not specified
  if (View == None):
    View = Grid.MainView

  # Clear the previous selection
  View.ClearSelection()

  # Select the specified range of rows
  View.SelectRange (StartRowIndex, EndRowIndex)

VBScript

Sub Main
  Dim p, frmMain, Grid, RowIndexes

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridTutorials")
  Set frmMain = p.WinFormsObject("frmMain")
  ' Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' Select a range of rows
  Call SelectRange_2 (Grid, Nothing, 8, 14)

  aqUtils.Delay 1000

  ' Select several rows
  RowIndexes = Array (1, 2, 4, 7, 11)
  Call SelectRows_2 (Grid, Nothing, RowIndexes)
End Sub

Sub SelectRows_2 (Grid, View, RowIndexes)
  ' Get the grid view object, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If

  ' Clear the previous selection
  View.ClearSelection

  ' Select the specified rows
  For i = 0 To UBound(RowIndexes)
    View.SelectRow (RowIndexes(i))
  Next
End Sub

Sub SelectRange_2 (Grid, View, StartRowIndex, EndRowIndex)
  ' Get the grid view object, if it is not specified
  If View Is Nothing Then
    Set View = Grid.MainView
  End If

  ' Clear the previous selection
  View.ClearSelection

  ' Select the specified range of rows
  Call View.SelectRange (StartRowIndex, EndRowIndex)
End Sub

DelphiScript

procedure SelectRows_2 (Grid, View, RowIndexes); forward;
procedure SelectRange_2 (Grid, View, StartRowIndex, EndRowIndex); forward;

procedure Main;
var p, frmMain, Grid, RowIndexes : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridTutorials');
  frmMain := p.WinFormsObject('frmMain');
  // Select the 'Multi Select' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Multi Select';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Select a range of rows
  SelectRange_2 (Grid, nil, 8, 14);

  aqUtils.Delay (1000);

  // Select several rows
  RowIndexes := CreateVariantArray (0, 4);
  RowIndexes[0] := 1; RowIndexes[1] := 2;
  RowIndexes[2] := 4; RowIndexes[3] :=7;
  RowIndexes[4] := 11;
  SelectRows_2 (Grid, nil, RowIndexes);
end;

procedure SelectRows_2 (Grid, View, RowIndexes);
var i : OleVariant;
begin
  // Get the grid view object, if it is not specified
  if View = nil then
    View := Grid.MainView;

  // Clear the previous selection
  View.ClearSelection;

  // Select the specified rows
  for i := 0 to VarArrayHighBound(RowIndexes, 1) do
    View.SelectRow (RowIndexes[i]);
end;

procedure SelectRange_2 (Grid, View, StartRowIndex, EndRowIndex);
begin
  // Get the grid view object, if it is not specified
  if View = nil then
    View := Grid.MainView;

  // Clear the previous selection
  View.ClearSelection;

  // Select the specified range of rows
  View.SelectRange (StartRowIndex, EndRowIndex);
end;

C++Script, C#Script

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

  // Obtain the application process and its main form
  p = Sys["Process"]("GridTutorials");
  frmMain = p["WinFormsObject"]("frmMain");
  // Select the "Multi Select" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Multi Select";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Select a range of rows
  SelectRange_2 (Grid, null, 8, 14);

  aqUtils["Delay"] (1000);

  // Select several rows
  RowIndexes = new Array (1, 2, 4, 7, 11);
  SelectRows_2 (Grid, null, RowIndexes);
}

function SelectRows_2 (Grid, View, RowIndexes)
{
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid["MainView"];

  // Clear the previous selection
  View["ClearSelection"]();

  // Select the specified rows
  for (var i=0; i<RowIndexes["length"]; i++)
    View["SelectRow"](RowIndexes[i]);
}

function SelectRange_2 (Grid, View, StartRowIndex, EndRowIndex)
{
  // Get the grid view object, if it is not specified
  if (View == null)
    View = Grid["MainView"];

  // Clear the previous selection
  View["ClearSelection"]();

  // Select the specified range of rows
  View["SelectRange"](StartRowIndex, EndRowIndex);
}

See Also

Working With Developer Express XtraGrid
Accessing Views in Developer Express XtraGrid
Selecting Cells in Developer Express XtraGrid
Obtaining Selected Records in Developer Express XtraGrid
ClickRowIndicator Action (Grid Controls)

Highlight search results