Obtaining Selected Records in Developer Express XtraGrid

Applies to TestComplete 14.10, last modified on June 5, 2019

When testing your application with the XtraGrid control, you may need to select multiple records in a grid. Then, you may need to determine which records are selected in the grid in order to process them a certain way. You can work with the selected records using the following native properties and methods of the grid’s view:

  • viewObj.GetSelectedRows - Returns an array containing the indexes of the rows (cards) selected in the given view. Note that the returned array is a .NET object, so you cannot access its elements using simple indexing. You should use the object’s internal properties or methods instead.
  • viewObj.SelectedRowsCount - Returns the number of rows (cards) currently selected in the given view.
Note: These properties are available to TestComplete only if the .NET Application Support plugin is installed and enabled.

Below is an example that demonstrates how you can work with selections in the XtraGrid control. This example works with the GridTutorials sample application shipped with the Developer Express XtraGrid Suite.

Example

View description

JavaScript

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

  // Select a range of rows
  SelectRange (Grid, 3, 21);

  // Iterate through the selected rows
  let RowIndexes = GetSelectedRowIndexes (Grid, null);
  for (let i=0; i<RowIndexes.length; i++)
    Log.Message (RowIndexes[i] + ": " + Grid.wValue(RowIndexes[i], "Order ID"));
}

// 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);
}

// Returns an array of selected rows' indexes
function GetSelectedRowIndexes (Grid, View)
{
  // Get the grid view object, if it is not specified
  if (strictEqual(View, null))
    View = Grid.MainView;

  // Get an array of selected rows' indexes
  let SelectedRows = View.GetSelectedRows();

  // Convert a .NET array to a JavaScript array
  let arr = new Array (View.SelectedRowsCount);
  for (let i=0; i<View.SelectedRowsCount; i++)
    arr[i] = SelectedRows.Get(i);

  return arr;
}

JScript

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

  // 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, 3, 21);

  // Iterate through the selected rows
  RowIndexes = GetSelectedRowIndexes (Grid, null);
  for (i=0; i<RowIndexes.length; i++)
    Log.Message (RowIndexes[i] + ": " + Grid.wValue(RowIndexes[i], "Order ID"));
}

// 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);
}

// Returns an array of selected rows' indexes
function GetSelectedRowIndexes (Grid, View)
{
  var SelectedRows, arr, i;

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

  // Get an array of selected rows' indexes
  SelectedRows = View.GetSelectedRows();

  // Convert a .NET array to a JScript array
  arr = new Array (View.SelectedRowsCount);
  for (i=0; i<View.SelectedRowsCount; i++)
    arr[i] = SelectedRows.Get(i);

  return arr;
}

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, 3, 21)
  
  # Iterate through the selected rows
  RowIndexes = GetSelectedRowIndexes (Grid, None)
  for i in range(0, RowIndexes.length):
    Log.Message (RowIndexes[i] + ": " + Grid.wValue(RowIndexes[i], "Order ID"))

# 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)

# Returns an array of selected rows' indexes
def GetSelectedRowIndexes (Grid, View):

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

  # Get an array of selected rows' indexes
  SelectedRows = View.GetSelectedRows()

  # Convert a .NET array to a Python array
  arr = list(View.SelectedRowsCount)
  for i in range(0, View.SelectedRowsCount-1):
    arr[i] = SelectedRows.Get(i)

  return arr

VBScript

Sub Main
  Dim p, frmMain, Grid, RowIndexes, i

  ' 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, 3, 21)
  
  ' Iterate through the selected rows
  RowIndexes = GetSelectedRowIndexes (Grid, Nothing)
  For i = 0 To UBound(RowIndexes)
    Log.Message RowIndexes(i) & ": " & Grid.wValue(RowIndexes(i), "Order ID")
  Next
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

' Returns an array of selected rows' indexes
Function GetSelectedRowIndexes (Grid, View)
  Dim SelectedRows, arr, i

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

  ' Get an array of selected rows' indexes
  Set SelectedRows = View.GetSelectedRows

  ' Convert a .NET array to a Variant array
  arr = CreateVariantArray (0, View.SelectedRowsCount-1)
  For i = 0 To View.SelectedRowsCount-1
    arr(i) = SelectedRows.Get(i)
  Next

  GetSelectedRowIndexes = arr
End Function

DelphiScript

procedure SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex : OleVariant); forward;
function GetSelectedRowIndexes (Grid, View); forward;

procedure Main;
var p, frmMain, Grid, RowIndexes, i : 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, 3, 21);

  // Iterate through the selected rows
  RowIndexes := GetSelectedRowIndexes (Grid, nil);
  for i := 0 to VarArrayHighBound(RowIndexes, 1) do
    Log.Message (aqConvert.VarToStr(RowIndexes[i]) + ': ' + aqConvert.VarToStr(Grid.wValue[RowIndexes[i], 'Order ID']));
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;

// Returns an array of selected rows' indexes
function GetSelectedRowIndexes (Grid, View);
var SelectedRows, arr, i : OleVariant;
begin
  // Get the grid view object, if it is not specified
  if View = nil then
    View := Grid.MainView;

  // Get an array of selected rows' indexes
  SelectedRows := View.GetSelectedRows;

  // Convert a .NET array to a Variant array
  arr := CreateVariantArray (0, View.SelectedRowsCount - 1);
  for i := 0 to View.SelectedRowsCount-1 do
    arr[i] := SelectedRows.Get(i);

  Result := arr;
end;

C++Script, C#Script

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

  // 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, 3, 21);

  // Iterate through the selected rows
  RowIndexes = GetSelectedRowIndexes (Grid, null);
  for (i=0; i<RowIndexes["length"]; i++)
    Log["Message"](RowIndexes[i] + ": " + Grid["wValue"](RowIndexes[i], "Order ID"));
}

// 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);
}

// Returns an array of selected rows' indexes
function GetSelectedRowIndexes (Grid, View)
{
  var SelectedRows, arr, i;

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

  // Get an array of selected rows' indexes
  SelectedRows = View["GetSelectedRows"]();

  // Convert a .NET array to a JScript array
  arr = new Array (View["SelectedRowsCount"]);
  for (i=0; i<View["SelectedRowsCount"]; i++)
    arr[i] = SelectedRows["Get"](i);

  return arr;
}

See Also

Working With Developer Express XtraGrid
Accessing Views in Developer Express XtraGrid
Obtaining and Setting Cell Values in Developer Express XtraGrid
Selecting Cells in Developer Express XtraGrid
Selecting Multiple Rows and Cards in Developer Express XtraGrid

Highlight search results