Selecting Multiple Rows in Infragistics UltraGrid

Applies to TestComplete 15.68, last modified on October 16, 2024

When working with UltraGrid controls, you may need to simulate the selection of several grid rows and perform some actions over the selected rows. This topic explains how to select one or more grid rows. To learn how to get values from selected rows, see the Obtaining Selected Elements in Infragistics UltraGrid topic.

You can influence the data rows in the same manner as data cells. Infragistics UltraGrid allows you to select one or more rows that belong to the same hierarchical level. If a row is selected then it is marked with special highlighting.

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

To perform these actions, TestComplete should have access to internal objects, properties and methods of the UltraGrid control. For this purpose, the .NET Application Support plugin must be installed and enabled.

When testing Infragistics UltraGrid controls, use specific methods and properties of the corresponding Infragistics UltraGrid 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 a UltraGrid 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

Normally, the users can select multiple rows 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 holding no key. Clears the previous selection and selects the clicked row.
Clicking a row holding the Shift key. Clears the previous selection and selects the range of rows between the clicked row and the previously selected ones.
Clicking a row holding the Ctrl key. Selects the clicked row, preserving the previous selection.
Clicking a row holding both the Ctrl and Shift keys. Selects a range of rows between the previously selected row(s) and the clicked one, preserving the previous selection.

To simulate a click on a row indicator, you can use the ClickRowIndicator action of the Infragistics UltraGrid and InfragisticsUltraGridView objects. The action of the Infragistics UltraGrid object apply to rows of the main data view, whereas the InfragisticsUltraGridView action affect the rows of the assosiated child data view. The parameters of the ClickRowIndicator action specifies the index of the row to be clicked, and also whether the SHIFT, CTRL, ALT or combination of these keys should be pressed during the click.

Example

View description

JavaScript, JScript

function RowSelectionWithMouse()
{
  var p, Grid, RowIndexes
  // Obtain the application process
  p = Sys.Process("SamplesExplorer");
  // Select the "Merged Cells Feature" demo
  p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V5.1 Merged Cells Feature");
  // Obtain the grid control
  Grid = p.frmMergedCells.WinFormsObject("UltraGrid1");
  
  // Select multiple rows
  RowIndexes = new Array (0, 2, 4, 6, 8);
  SelectRows (Grid, RowIndexes);

  // Select a range of rows
  SelectRowRange (Grid, 1, 5);
}

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

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

Python

def RowSelectionWithMouse():
  # Obtain the application process
  p = Sys.Process("SamplesExplorer")
  # Select the "Merged Cells Feature" demo
  p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V5.1 Merged Cells Feature")
  # Obtain the grid control
  Grid = p.frmMergedCells.WinFormsObject("UltraGrid1")
  
  # Select multiple rows
  RowIndexes = list(0, 2, 4, 6, 8)
  SelectRows (Grid, RowIndexes)

  # Select a range of rows
  SelectRowRange (Grid, 1, 5)

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

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

VBScript

Sub RowSelectionWithMouse
  Dim p, Grid, RowIndexes
  ' Obtain the application process
  Set p = Sys.Process("SamplesExplorer")
  ' Select the "Merged Cells Feature" demo
  Call p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V5.1 Merged Cells Feature")
  ' Obtain the grid control
  Set Grid = p.frmMergedCells.WinFormsObject("UltraGrid1")
  
  ' Select multiple rows
  RowIndexes = Array (0, 2, 4, 6, 8)
  Call SelectRows (Grid, RowIndexes)

  ' Select a range of rows
  Call SelectRowRange (Grid, 1, 5)
End Sub

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

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

DelphiScript

procedure SelectRows (DataView, RowIndexes); forward;
procedure SelectRowRange (DataView, StartRowIndex, EndRowIndex); forward;

procedure RowSelectionWithMouse;
var p, Grid, RowIndexes :OleVariant;
begin
  // Obtain the application process
  p := Sys.Process('SamplesExplorer');
  // Select the "Merged Cells Feature" demo
  p.frmMain.WinFormsObject('tvwSamples').DblClickItem('|Feature Samples|V5.1 Merged Cells Feature');
  // Obtain the grid control
  Grid := p.frmMergedCells.WinFormsObject('UltraGrid1');
  
  // Select multiple rows
  RowIndexes := CreateVariantArray (0, 4);
  RowIndexes[0] := 0; RowIndexes[1] := 2;
  RowIndexes[2] := 4; RowIndexes[3] :=6;
  RowIndexes[4] := 9;

  SelectRows (Grid, RowIndexes);

  // Select a range of rows
  SelectRowRange (Grid, 1, 5);
end;

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

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

C++Script, C#Script

function RowSelectionWithMouse()
{
  var p, Grid, RowIndexes
  // Obtain the application process
  p = Sys["Process"]("SamplesExplorer");
  // Select the "Merged Cells Feature" demo
  p["frmMain"]["WinFormsObject"]("tvwSamples")["DblClickItem"]("|Feature Samples|V5.1 Merged Cells Feature");
  // Obtain the grid control
  Grid = p["frmMergedCells"]["WinFormsObject"]("UltraGrid1");
  
  // Select multiple rows
  RowIndexes = new Array (0, 2, 4, 6, 8);
  SelectRows (Grid, RowIndexes);

  // Select a range of rows
  SelectRowRange (Grid, 1, 5);
}

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

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

Using Internal Methods of the UltraGrid Control

The selected state of the row is controlled by the native UltraGridRow.Selected property. To obtain an UltraGridRow object that corresponds to a row, use the GetRow method that is provided in the Accessing Grid Elements in Infragistics UltraGrid topic. It returns the row that belongs to a specified grid and bands it with the specified index.

The UltraGridRow.Selected read-write property can accept Boolean values that specify whether a row is selected or not. Note however that the UltraGrid control allows you to select multiple rows and modifying the Selected property of a row does not affect the rows selected earlier. This could be helpful when you need to select several rows, but on the other hand, it could be confusing when you want to select a single row. To discard previously selected rows call the UltraGridObj.Selected.Rows.Clear method. This method is provided by the SelectedRowsCollection object that represents a collection of selected rows. See the Obtaining Selected Elements in Infragistics UltraGrid topic to read more about collections of selected grid elements.

Example

View description

JavaScript

function NativeRowSelection()
{
  var Grid, RowIdx;
  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 (let 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.");

  // Remove selection from one of the rows
  if (UnselectRow(Grid, null, RowIdx))
    Log.Message ("The row " + RowIdx + " was deselected.")
  else
    Log.Message ("The row " + RowIdx + " cannot be deselected.");
}

function SelectRow(Grid, ChildView, RowIndex)
{
  // Obtain the row object
  Row = GetRow(Grid, ChildView, RowIndex);

  if (!strictEqual(Row, null))
  {
    Row.Selected = true;
    return true;
  }
  return false;
}

function UnselectRow(Grid, ChildView, RowIndex)
{
  // Obtain the row object
  Row = GetRow(Grid, ChildView, RowIndex);

  if (!strictEqual(Row, null))
  {
    Row.Selected = false;
    return true;
  }
  return false;
}

JScript

function NativeRowSelection()
{
  var Grid, RowIdx;
  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.");

  // Remove selection from one of the rows
  if (UnselectRow(Grid, null, RowIdx))
    Log.Message ("The row " + RowIdx + " was deselected.")
  else
    Log.Message ("The row " + RowIdx + " cannot be deselected.");
}

function SelectRow(Grid, ChildView, RowIndex)
{
  // Obtain the row object
  Row = GetRow(Grid, ChildView, RowIndex);

  if (Row != null)
  {
    Row.Selected = true;
    return true;
  }
  return false;
}

function UnselectRow(Grid, ChildView, RowIndex)
{
  // Obtain the row object
  Row = GetRow(Grid, ChildView, RowIndex);

  if (Row != null)
  {
    Row.Selected = false;
    return true;
  }
  return false;
}

Python

def NativeRowSelection():
  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.")

  # Remove selection from one of the rows
  if (UnselectRow(Grid, None, RowIdx)):
    Log.Message ("The row " + RowIdx + " was deselected.")
  else:
    Log.Message ("The row " + RowIdx + " cannot be deselected.")

def SelectRow(Grid, ChildView, RowIndex):
  # Obtain the row object
  Row = GetRow(Grid, ChildView, RowIndex)

  if (Row != None):
    Row.Selected = True
    return True
  return False

def UnselectRow(Grid, ChildView, RowIndex):
  # Obtain the row object
  Row = GetRow(Grid, ChildView, RowIndex)

  if (Row != None):
    Row.Selected = False
    return True
  return False

VBScript

Sub NativeRowSelection
  Dim Grid, RowIdx
  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

  ' Remove selection from one of the rows
  If UnselectRow(Grid, Nothing, RowIdx) Then
    Log.Message ("The row " & CStr(RowIdx) & " was deselected.")
  Else
    Log.Message ("The row " & CStr(RowIdx) & " cannot be deselected.")
  End If
End Sub

Function SelectRow(Grid, ChildView, RowIndex)
  ' Set initial value for function
  SelectRow = False
  ' Obtain the row object
  Set Row = GetRow(Grid, ChildView, RowIndex)
  If Not Row Is Nothing Then
    Row.Selected = True
    SelectRow = True
  End If
End Function

Function UnselectRow(Grid, ChildView, RowIndex)
  ' Set initial value for function
  UnselectRow = False
  ' Obtain the row object
  Set Row = GetRow(Grid, ChildView, RowIndex)
  If Not Row Is Nothing Then
    Row.Selected = False
    UnselectRow = True
  End If
End Function

DelphiScript

function NativeRowSelection;
var Grid, 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.');
  
  // Remove selection from one of the rows
  if UnselectRow(Grid, nil, RowIdx) then 
    Log.Message ('The row ' + aqConvert.IntToStr(RowIdx) + ' was deselected.')
  else
    Log.Message ('The row ' + aqConvert.IntToStr(RowIdx) + ' cannot be deselected.');
end;

function SelectRow(Grid, ChildView, RowIndex): Variant;
var Row: OleVariant;
begin
  // Set initial value for function
  Result := False;
  // Obtain the row object
  Row := GetRow (Grid, ChildView, RowIndex);
  if Row <> nil then
  begin
    Row.Selected := true;
    Result := true
  end
end;

function UnselectRow(Grid, ChildView, RowIndex): Variant;
var Row: OleVariant;
begin
  // Set initial value for function
  Result := False;
  // Obtain the row object
  Row := GetRow (Grid, ChildView, RowIndex);
  if Row <> nil then
  begin
    Row.Selected := false;
    Result := true
  end
end;

C++Script, C#Script

function NativeRowSelection()
{
  var Grid, RowIdx;
  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.");

  // Remove selection from one of the rows
  if (UnselectRow(Grid, null, RowIdx))
    Log["Message"]("The row " + RowIdx + " was deselected.")
  else
    Log["Message"]("The row " + RowIdx + " cannot be deselected.");
}

function SelectRow(Grid, ChildView, RowIndex)
{
  // Obtain the row object
  Row = GetRow (Grid, ChildView, RowIndex);

  if (Row != null)
  {
    Row["Selected"] = true;
    return true;
  }
  return false;
}

function UnselectRow(Grid, ChildView, RowIndex)
{
  // Obtain the row object
  Row = GetRow (Grid, ChildView, RowIndex);

  if (Row != null)
  {
    Row["Selected"] = false;
    return true;
  }
  return false;
}

See Also

Working With Infragistics UltraGrid
Accessing Grid Elements in Infragistics UltraGrid
Expanding and Collapsing Rows in Infragistics UltraGrid
Obtaining Selected Elements in Infragistics UltraGrid
Selecting Cells in Infragistics UltraGrid
ClickRowIndicator Action (Grid Controls)

Highlight search results