Obtaining Selected Elements in Infragistics UltraGrid

Applies to TestComplete 15.44, last modified on November 10, 2022

When working with UltraGrid controls, you may need to simulate the selection of several grid cells, columns or rows and perform actions over the elements added to selection. This topic explains how to obtain values from the selected elements. To learn how to select grid cells and rows, see Selecting Cells in Infragistics UltraGrid and Selecting Multiple Rows in Infragistics UltraGrid topics.

To manage the selected elements, UltraGrid has a special collection that is accessible via the Selected property. The returned collection object, in its turn, has the Rows, Columns and Cells properties that provide access to collections of selected rows, columns and cells. Note, that the objects in the collections are arranged according to the order in which they were selected. Each of those collections have a similar set of methods and properties that are intended to operate with the corresponding grid elements.

The table below lists the most important properties and methods of the SelectedRowsCollection, SelectedColsCollection, SelectedCellsCollection collections.

Property, Method Description
Count Read-only property. Returns the number of selected elements of a particular type.
Item Read-only property. Returns an element with the specified index.
Add Adds the specified element to the selection.
AddRange Adds several elements to the selection. The elements are passed via an array.
Clear Removes all elements from selection.
In order for TestComplete to access these properties and methods, the .NET Application Support plugin must be installed and enabled.
Example

This example demonstrates how to obtain an array of selected rows and iterate through it to get the values of the desired cells. Some auxiliary routines of this example are located in other topics. You can find their descriptions and links to them in the expandable section below.

View description

JavaScript, JScript

function GetSelectedRows(grid)
{
  var i, SelectedRows;

  SelectedRows = new Array (grid.Selected.Rows.Count);
  // Iterate through a collection of selected rows
  for (i = 0; i < grid.Selected.Rows.Count; i++)
    SelectedRows[i] = grid.Selected.Rows.Item(i);

  return SelectedRows;
}

function TestRowSelection()
{
  var Grid, SelRows, RowIdx, i;
  RowIdx=0;

  // Obtain the grid object
  Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmEmptyRows").WinFormsObject("UltraGrid1");
  
  // Discard previously selected rows (if any)
  Grid.Selected.Rows.Clear;
  
  // Select several rows
  for (i=0; i<=2; i++)
    if (SelectRow(Grid, null, RowIdx+i))
      Log.Message ("The row " + (RowIdx+i) + " was added to selection.")
    else
      Log.Message ("The row " + (RowIdx+i) + " cannot be added to selection.")

  // Get an array of selected rows
  SelRows = GetSelectedRows(Grid);
  for (i=0; i < SelRows.length; i++)
    Log.Message (GetCellValue(Grid, null, SelRows[i].Index, "Column 2"));
}

Python

def GetSelectedRows(grid):

  SelectedRows = list(grid.Selected.Rows.Count)
  # Iterate through a collection of selected rows
  for i in range(0, grid.Selected.Rows.Count-1):
    SelectedRows[i] = grid.Selected.Rows.Item[i]

  return SelectedRows

def TestRowSelection():
  RowIdx=0

  # Obtain the grid object
  Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmEmptyRows").WinFormsObject("UltraGrid1")
  
  # Discard previously selected rows (if any)
  Grid.Selected.Rows.Clear
  
  # Select several rows
  for i in range(0, 2): 
    if (SelectRow(Grid, None, RowIdx+i)): 
      Log.Message ("The row " + (RowIdx+i) + " was added to selection.")
    else:
      Log.Message ("The row " + (RowIdx+i) + " cannot be added to selection.")

  # Get an array of selected rows
  SelRows = GetSelectedRows(Grid)
  for i in range(0, SelRows.length-1):
    Log.Message (GetCellValue(Grid, None, SelRows[i].Index, "Column 2"))

VBScript

Function GetSelectedRows(grid)
  SelectedRows = CreateVariantArray (0, Grid.Selected.Rows.Count - 1)
  ' Iterate through a collection of selected rows
  For i = 0 To grid.Selected.Rows.Count - 1
    Set SelectedRows(i) = grid.Selected.Rows.Item(i)
  Next
  GetSelectedRows = SelectedRows
End Function

Sub TestRowSelection
Dim Grid, SelRows
  RowIdx=0

  ' Obtain the grid object
  Set Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmEmptyRows").WinFormsObject("UltraGrid1")
  
  ' Discard previously selected rows (if any)
  Grid.Selected.Rows.Clear

  ' Select several rows
  For i=0 To 2
    If SelectRow(Grid, Nothing, RowIdx+i) Then
      Log.Message ("The row " & CStr(RowIdx+i) & " was added to selection.")
    Else
      Log.Message ("The row " & CStr(RowIdx+i) & " cannot be added to selection.")
    End If
  Next

  ' Get an array of selected rows
  SelRows=GetSelectedRows(Grid)
  For i = 0 To VarArrayHighBound(SelRows, 1)
    Log.Message (GetCellValue(Grid, Nothing, SelRows(i).Index, "Column 2"))
   Next
End Sub

DelphiScript

function GetSelectedRows(grid): Variant;
var SelectedRows, i : OleVariant;
begin
  SelectedRows := CreateVariantArray (0, grid.Selected.Rows.Count - 1);
  // Iterate through a collection of selected rows
  for i := 0 to grid.Selected.Rows.Count - 1 do
    SelectedRows[i] := grid.Selected.Rows.Item[i];
  Result := SelectedRows;
end;

function TestRowSelection;
var Grid, SelRows, i, RowIdx: OleVariant;
begin
  RowIdx:=0;

  // Obtain the grid object
  Grid := Sys.Process('SamplesExplorer').WinFormsObject('frmEmptyRows').WinFormsObject('UltraGrid1');

  // Discard previously selected rows (if any)
  Grid.Selected.Rows.Clear;

  // Select several rows
  for i:=0 to 2 do 
    if SelectRow (Grid, nil, RowIdx+i) then 
      Log.Message ('The row ' + aqConvert.IntToStr(RowIdx+i )+ ' was added to selection.')
    else
      Log.Message ('The row ' + aqConvert.IntToStr(RowIdx+i) + ' cannot be added to selection.');

  // Get an array of selected rows
  SelRows := GetSelectedRows(grid);
  for i := 0 to VarArrayHighBound(SelRows, 1) do
    Log.Message (GetCellValue(grid, nil, SelRows[i].Index, 'Column 2'));
end;

C++Script, C#Script

function GetSelectedRows(grid)
{
  var i, SelectedRows;

  SelectedRows = new Array (grid["Selected"]["Rows"]["Count"]);
  // Iterate through a collection of selected rows
  for (i = 0; i < grid["Selected"]["Rows"]["Count"]; i++)
    SelectedRows[i] = grid["Selected"]["Rows"]["Item"](i);

  return SelectedRows;
}

function TestRowSelection()
{
  var Grid, SelRows, RowIdx, i;
  RowIdx=0;

  // Obtain the grid object
  Grid = Sys["Process"]("SamplesExplorer")["WinFormsObject"]("frmEmptyRows")["WinFormsObject"]("UltraGrid1");

  // Discard previously selected rows (if any)
  Grid["Selected"]["Rows"]["Clear"];

  // Select several rows
  for (i=0; i<=2; i++)
    if (SelectRow(Grid, null, RowIdx+i))
      Log["Message"]("The row " + (RowIdx+i) + " was added to selection.")
    else
      Log["Message"]("The row " + (RowIdx+i) + " cannot be added to selection.")

  // Get an array of selected rows
  SelRows = GetSelectedRows(Grid);
  for (i=0; i < SelRows["length"]; i++)
    Log["Message"](GetCellValue(Grid, null, SelRows[i]["Index"], "Column 2"));
}

See Also

Working With Infragistics UltraGrid
Accessing Grid Elements in Infragistics UltraGrid
Selecting Multiple Rows in Infragistics UltraGrid
Expanding and Collapsing Rows in Infragistics UltraGrid
Obtaining and Setting Cell Values in Infragistics UltraGrid

Highlight search results