Obtaining Selected Rows in Syncfusion GridGroupingControl

Applies to TestComplete 15.42, last modified on September 08, 2022

When testing your application with Syncfusion GridGroupingControl, you may need to select multiple rows in a grid. Then, you may need to determine which rows are selected in the grid in order to process them a certain way. You can obtain the selected rows using the following statements:

GridObj.Table.SelectedRecords

GridObj.GetTable(TableName).SelectedRecords

The first statement provides access to the collection of selected rows in a grid’s root table. The second statement returns the collection of rows selected in a child table (the table name is specified by the TableName parameter).

To get the number of selected rows, use the Count property of the obtained collection. Using the Item(Index).Record property you can get an individual grid row by its zero-based index within the selected rows collection. To determine the index of the selected record within the grid table, use one of the statements below (the first one can be used for the root table, the second one can be used for child tables):

GridObj.Table.Records.IndexOf(RecordObj)

GridObj.GetTable(TableName).Records.IndexOf(RecordObj)

In order for TestComplete to access the mentioned properties and methods, the .NET Application Support plugin must be installed and enabled.

After you have obtained the selected grid rows, you can process them in a specific way. For example, you can iterate through them and obtain their cell values. Below is an example that illustrates how you can do this.

Example

View description

JavaScript

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

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

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

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

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

// Returns an array of selected rows' indexes
function GetSelectedRowIndexes (Grid, TableOrGroup)
{
  var Table, SelectedRecords, arr;

  // Get the selected records collection
  if (strictEqual(TableOrGroup, null))
  {
    TableOrGroup = Grid.Table;
    SelectedRecords = Grid.Table.SelectedRecords;
  }
  else
    SelectedRecords = Grid.GetTable(TableOrGroup.ParentTableDescriptor.Name).SelectedRecords;

  // Create an array and fill it with rows' indexes
  arr = new Array (SelectedRecords.Count);
  for (let i=0; i<SelectedRecords.Count; i++)
    arr[i] = TableOrGroup.Records.IndexOf( SelectedRecords.Item(i).Record );

  return arr;
}

JScript

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

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

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

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

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

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

  // Get the selected records collection
  if (TableOrGroup == null)
  {
    TableOrGroup = Grid.Table;
    SelectedRecords = Grid.Table.SelectedRecords;
  }
  else
    SelectedRecords = Grid.GetTable(TableOrGroup.ParentTableDescriptor.Name).SelectedRecords;

  // Create an array and fill it with rows' indexes
  arr = new Array (SelectedRecords.Count);
  for (i=0; i<SelectedRecords.Count; i++)
    arr[i] = TableOrGroup.Records.IndexOf( SelectedRecords.Item(i).Record );

  return arr;
}

Python

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

  # Click cell in a root table
  Grid.ClickCell (0, "LastName")
  # Enter the new value
  Grid.Keys ("[Home]![End][Del]" + "Smith" + "[Enter]")

  # Click cell in a nested table
  Grid.wChildView[0, 1].wChildView[0, 0].ClickCell(0, "Quantity")
  # Enter the new value
  Grid.Keys ("[Home]![End][Del]" + "75" + "[Enter]")

VBScript

Sub Main
  Dim p, Grid, RowIndexes, i

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

  ' Select a range of rows
  Call SelectRange (Grid, 7, 21)

  ' Obtain the selected rows
  RowIndexes = GetSelectedRowIndexes (Grid, Nothing)
  ' Iterate through the selected rows
  For i = 0 To UBound(RowIndexes)
    Log.Message ("CompanyName: " & Grid.wValue(RowIndexes(i), "CompanyName").ToString.OleValue)
  Next
End Sub

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

' Returns an array of selected rows' indexes
Function GetSelectedRowIndexes (Grid, TableOrGroup)
  Dim Table, SelectedRecords, arr, i

  ' Get the selected records collection
  If TableOrGroup Is Nothing Then
    Set TableOrGroup = Grid.Table
    Set SelectedRecords = Grid.Table.SelectedRecords
  Else
    Set SelectedRecords = Grid.GetTable(TableOrGroup.ParentTableDescriptor.Name).SelectedRecords
  End If

  ' Create an array and fill it with rows' indexes
  arr = CreateVariantArray(0, SelectedRecords.Count-1)
  For i = 0 To SelectedRecords.Count-1
    arr(i) = TableOrGroup.Records.IndexOf(SelectedRecords.Item(i).Record)
  Next

  GetSelectedRowIndexes = arr
End Function

DelphiScript

procedure SelectRange (View, StartRowIndex, EndRowIndex); forward;
function GetSelectedRowIndexes (Grid, TableOrGroup); forward;

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

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

  // Obtain the selected rows
  RowIndexes := GetSelectedRowIndexes (Grid, nil);
  // Iterate through the selected rows
  for i := 0 to VarArrayHighBound(RowIndexes, 1)-1 do
    Log.Message ('CompanyName: ' + Grid.wValue[RowIndexes[i], 'CompanyName'].ToString.OleValue)
end;

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

// Returns an array of selected rows' indexes
function GetSelectedRowIndexes (Grid, TableOrGroup);
var Table, SelectedRecords, arr, i : OleVariant;
begin
  // Get the selected records collection
  if TableOrGroup = nil then
  begin
    TableOrGroup := Grid.Table;
    SelectedRecords := Grid.Table.SelectedRecords;
  end
  else
    SelectedRecords := Grid.GetTable(TableOrGroup.ParentTableDescriptor.Name).SelectedRecords;

  // Create an array and fill it with rows' indexes
  arr := CreateVariantArray(0, SelectedRecords.Count-1);
  for i := 0 to SelectedRecords.Count-1 do
    arr[i] := TableOrGroup.Records.IndexOf(SelectedRecords.Item[i].Record);

  Result := arr;
end;

C++Script, C#Script

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

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

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

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

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

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

  // Get the selected records collection
  if (TableOrGroup == null)
  {
    TableOrGroup = Grid["Table"];
    SelectedRecords = Grid["Table"]["SelectedRecords"];
  }
  else
    SelectedRecords = Grid["GetTable"](TableOrGroup["ParentTableDescriptor"]["Name"])["SelectedRecords"];

  // Create an array and fill it with rows' indexes
  arr = new Array (SelectedRecords["Count"]);
  for (i=0; i<SelectedRecords["Count"]; i++)
    arr[i] = TableOrGroup["Records"]["IndexOf"]( SelectedRecords["Item"](i)["Record"] );

  return arr;
}

See Also

Working With Syncfusion GridGroupingControl
Selecting Cells in Syncfusion GridGroupingControl
Selecting Multiple Rows in Syncfusion GridGroupingControl
Obtaining and Setting Cell Values in Syncfusion GridGroupingControl
Iterating Through Rows in Syncfusion GridGroupingControl

Highlight search results