Obtaining Selected Rows in Syncfusion GridDataBoundGrid

Applies to TestComplete 15.20, last modified on January 19, 2022

When testing your application with the GridDataBoundGrid control, 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 statement:

GridObj.Selections.GetSelectedRows( true, false )

GridObj is the object corresponding to the GridDataBoundGrid control. The first parameter of the GetSelectedRows method (True) specifies that only whole selected rows should be returned by the method. The second parameter (False) specifies that the returned ranges collection should not include the current cell.

This method returns the GridRangeInfoList object representing the collection of selected row ranges. To get the number of ranges in the collection, use the Count property. The Item property lets you access GridRangeInfo objects corresponding to individual ranges by their zero-based indexes within the collection. To determine which grid rows belong to a particular range object, you can use the Top and Bottom properties that hold indexes of the topmost and the lower-most rows of the given range.

Note: In order for TestComplete to access the mentioned properties and methods of the GridDataBoundGrid control and its helper objects, the .NET Application Support plugin must be installed and enabled.

After you have determined the indexes of the selected grid rows, you can process the rows 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 process the selected rows in the GridDataBoundGrid control.

Example

View description

JavaScript, JScript

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

  // Obtain the application process and the grid object
  p = Sys.Process ("DataBoundSortByDisplayMember");
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1");

  // Get selected rows' indexes
  RowIndexes = GetSelectedRowIndexes (Grid);

  // Iterate through the selected rows
  for (i=0; i<RowIndexes.length; i++)
    Log.Message (RowIndexes[i] + ": " + GetCellText(Grid, RowIndexes[i], "ProductName"));
}

function GetSelectedRowIndexes (Grid)
{
  var Ranges, Rng, Indexes, i, Idx;

  Indexes = new Array ();
  // Get the collection of selected row ranges
  Ranges = Grid.Selections.GetSelectedRows(true, false);
  // Iterate through the ranges collection
  for (i=0; i<Ranges.Count; i++)
  {
    Rng = Ranges.Item(i);
    // Get indexes of rows that belong to the current range
    for (Idx=Rng.Top; Idx<=Rng.Bottom; Idx++)
      Indexes.push(Idx);
  }

  return Indexes;
}

function GetCellText (Grid, RowIndex, ColumnId)
{
  var ColIndex = GetColIndexById (Grid, ColumnId);

  return Grid.Item(RowIndex,ColIndex).FormattedText.OleValue;
}

function GetColIndexById (Grid, ColumnId)
{
  if (aqObject.GetVarType(ColumnId) == varOleStr)
    return Grid.NameToColIndex(ColumnId)
  else
    return ColumnId;
}

Python

def Main ():

  # Obtain the application process and the grid object
  p = Sys.Process ("DataBoundSortByDisplayMember")
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1")

  # Get selected rows' indexes
  RowIndexes = GetSelectedRowIndexes (Grid)

  # Iterate through the selected rows
  for i in range(0, RowIndexes.length-1):
    Log.Message (RowIndexes[i] + ": " + GetCellText(Grid, RowIndexes[i], "ProductName")) 

def GetSelectedRowIndexes (Grid):

  Indexes = list()
  # Get the collection of selected row ranges
  Ranges = Grid.Selections.GetSelectedRows(True, False)
  # Iterate through the ranges collection
  for i in range(0, Ranges.Count-1):
    Rng = Ranges.Item[i]
    # Get indexes of rows that belong to the current range 
    for Idx in range(Rng.Top, Rng.Bottom):
      Indexes.push(Idx)

  return Indexes

def GetCellText (Grid, RowIndex, ColumnId):
  ColIndex = GetColIndexById (Grid, ColumnId)

  return Grid.Item(RowIndex,ColIndex).FormattedText.OleValue

def GetColIndexById (Grid, ColumnId):
  if (aqObject.GetVarType(ColumnId) == varOleStr):
    return Grid.NameToColIndex(ColumnId)
  else:
    return ColumnId

VBScript

Sub Main
  Dim p, Grid, RowIndexes, i

  ' Obtain the application process and the grid object
  Set p = Sys.Process ("DataBoundSortByDisplayMember")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1")

  ' Get selected rows' indexes
  RowIndexes = GetSelectedRowIndexes (Grid)

  ' Iterate through the selected rows
  For i=0 To UBound(RowIndexes)
    Call Log.Message (CStr(RowIndexes(i)) & ": " & GetCellText(Grid, RowIndexes(i), "ProductName"))
  Next
End Sub

Function GetSelectedRowIndexes (Grid)
  Dim Ranges, Rng, Indexes, i, j, Idx

  Indexes = Array ()

  ' Get the collection of selected row ranges
  Set Ranges = Grid.Selections.GetSelectedRows(True, False)

  ' Iterate through the ranges collection
  j = 0 ' Counter of Indexes elements
  For i=0 To Ranges.Count-1
    Set Rng = Ranges.Item(i)
    ' Increase the array size
    Redim Preserve Indexes (UBound(Indexes) + Rng.Height)
    ' Get indexes of rows that belong to the current range
    For Idx=Rng.Top To Rng.Bottom
      Indexes(j) = Idx
      j = j + 1
    Next
  Next

  GetSelectedRowIndexes = Indexes
End Function

Function GetCellText (Grid, RowIndex, ColumnId)
  Dim ColIndex
  ColIndex = GetColIndexById (Grid, ColumnId)

  GetCellText = Grid.Item(RowIndex,ColIndex).FormattedText.OleValue
End Function

Function GetColIndexById (Grid, ColumnId)
  If aqObject.GetVarType(ColumnId) = varOleStr Then
    GetColIndexById = Grid.NameToColIndex(ColumnId)
  Else
    GetColIndexById = ColumnId
  End If
End Function

DelphiScript

function GetSelectedRowIndexes (Grid); forward;
function GetCellText (Grid, RowIndex, ColumnId); forward;
function GetColIndexById (Grid, ColumnId); forward;

procedure Main;
var p, Grid, RowIndexes, i : OleVariant;
begin
  // Obtain the application process and the grid object
  p := Sys.Process ('DataBoundSortByDisplayMember');
  Grid := p.WinFormsObject('Form1').WinFormsObject('gridDataBoundGrid1');

  // Get selected rows' indexes
  RowIndexes := GetSelectedRowIndexes (Grid);

  // Iterate through the selected rows
  for i:=0 to VarArrayHighBound(RowIndexes, 1) do
    Log.Message (aqConvert.VarToStr(RowIndexes[i]) + ': ' + GetCellText(Grid, RowIndexes[i], 'ProductName'));
end;

function GetSelectedRowIndexes (Grid);
var Ranges, Rng, Indexes, i, j, Idx : OleVariant;
begin
  Indexes := CreateVariantArray(-1,-1);
  // Get the collection of selected row ranges
  Ranges := Grid.Selections.GetSelectedRows(true, false);
  // Iterate through the ranges collection
  j := 0; // Counter of Indexes elements
  for i:=0 To Ranges.Count-1 do
  begin
    Rng := Ranges.Item[i];
    // Increase the array size
    VarArrayRedim (Indexes, VarArrayHighBound(Indexes, 1) + Rng.Height);
    // Get indexes of rows that belong to the current range
    for Idx:=Rng.Top to Rng.Bottom do
    begin
      Indexes[j] := Idx;
      j := j + 1;
    end;
  end;

  Result := Indexes;
end;

function GetCellText (Grid, RowIndex, ColumnId);
var ColIndex : OleVariant;
begin
  ColIndex := GetColIndexById (Grid, ColumnId);
  Result := Grid.Item[RowIndex,ColIndex].FormattedText.OleValue;
end;

function GetColIndexById (Grid, ColumnId);
begin
  if aqObject.GetVarType(ColumnId) = varOleStr then
    Result := Grid.NameToColIndex(ColumnId)
  else
    Result := ColumnId;
end;

C++Script, C#Script

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

  // Obtain the application process and the grid object
  p = Sys["Process"]("DataBoundSortByDisplayMember");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridDataBoundGrid1");

  // Get selected rows' indexes
  RowIndexes = GetSelectedRowIndexes (Grid);

  // Iterate through the selected rows
  for (i=0; i<RowIndexes["length"]; i++)
    Log["Message"](RowIndexes[i] + ": " + GetCellText(Grid, RowIndexes[i], "ProductName"));
}

function GetSelectedRowIndexes (Grid)
{
  var Ranges, Rng, Indexes, i, Idx;

  Indexes = new Array ();
  // Get the collection of selected row ranges
  Ranges = Grid["Selections"]["GetSelectedRows"](true, false);
  // Iterate through the ranges collection
  for (i=0; i<Ranges["Count"]; i++)
  {
    Rng = Ranges["Item"](i);
    // Get indexes of rows that belong to the current range
    for (Idx=Rng["Top"]; Idx<=Rng["Bottom"]; Idx++)
      Indexes["push"](Idx);
  }

  return Indexes;
}

function GetCellText (Grid, RowIndex, ColumnId)
{
  var ColIndex = GetColIndexById (Grid, ColumnId);

  return Grid["Item"](RowIndex,ColIndex)["FormattedText"]["OleValue"];
}

function GetColIndexById (Grid, ColumnId)
{
  if (aqObject["GetVarType"](ColumnId) == varOleStr)
    return Grid["NameToColIndex"](ColumnId)
  else
    return ColumnId;
}

See Also

Working With Syncfusion GridDataBoundGrid
Accessing Rows, Columns and Cells in Syncfusion GridDataBoundGrid
Selecting Cells in Syncfusion GridDataBoundGrid
Selecting Multiple Rows in Syncfusion GridDataBoundGrid
Obtaining and Setting Cell Values in Syncfusion GridDataBoundGrid
Iterating Through Rows in Syncfusion GridDataBoundGrid

Highlight search results