Selecting Cells in Infragistics UltraGrid

Applies to TestComplete 15.69, last modified on November 13, 2024

One of the actions that you will perform the most over a grid is selecting cells. This topic describes several approaches that can be used to select a particular cell in the UltraGrid 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 Clicks on Cells

To simulate clicks and double-clicks on the grid cells the InfragisticsUltraGrid and InfragisticsUltraGridView objects have multiple Click actions. The actions of the InfragisticsUltraGrid object apply to the cells of the main data view, whereas the InfragisticsUltraGridView actions affect the cells of the assosiated child data view.

  • ClickCell and ClickCellR actions simulate a single click performed with the left or right mouse button respectively.
  • ClickCellXY and ClickCellRXY actions simulate a single click of a left or right mouse button that was performed at specific coordinates.
  • DblClickCell and DblClickCellR actions simulate a double click performed with the left or right mouse button.
  • DblClickCellXY and DblClickCellRXY actions simulate a double click of a left or right mouse button that was performed at specific coordinates.

All of these actions have parameters that specify the row index and the column’s caption or index. They also have an additional parameter that specifies the key or a combination of keys (CTRL, ALT, SHIFT) that are pressed during the simulation of a click.

Note: UltraGrid cells can have several actions associated with a mouse click. The current action is set by the column’s native property CellClickAction. For example, if this property is set to Edit, then the cell is selected and its in-place editor is automatically activated.

The code below demonstrates how to select a cell using a single mouse click inside the cell’s bounds. This example utilizes the grid from the “Fixed Headers” form of the “Samples Explorer” demo application that is provided with Infragistics UltraGrid. It selects a cell from the “FirstName” column of the sixth row that belongs to the main data view.

JavaScript, JScript

function Main1 ()
{
  var p, Grid, Cell, ColumnName, RowIdx;

  // Obtain the grid object
  p = Sys.Process("SamplesExplorer");
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");

  // Obtain and select the cell
  ColumnName = "FirstName";
  RowIdx = 5; // Note that index is zero-based

  // Simulate a click on a cell
  Grid.ClickCell(RowIdx, ColumnName);
}

Python

def Main1 ():

  # Obtain the grid object
  p = Sys.Process("SamplesExplorer")
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")

  # Obtain and select the cell
  ColumnName = "FirstName"
  RowIdx = 5 # Note that index is zero-based

  # Simulate a click on a cell
  Grid.ClickCell(RowIdx, ColumnName)

VBScript

Sub Main1
  Dim p, Grid, Cell, ColumnName, RowIdx

  ' Obtain the grid object
  Set p = Sys.Process("SamplesExplorer")
  Set Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")

  ' Obtain and select the cell
  ColumnName = "FirstName"
  RowIdx = 5 ' Note that index is zero-based

  ' Simulate a click on a cell
  Call Grid.ClickCell(RowIdx, ColumnName)
End Sub

DelphiScript

procedure Main1;
var p, Grid, Cell, ColumnName, RowIdx : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('SamplesExplorer');
  Grid := p.WinFormsObject('frmFixedHeaders').WinFormsObject('UltraGrid1');

  ColumnName := 'FirstName';
  RowIdx := 5; // Note that index is zero-based

  // Simulate a click on a cell
  Grid.Click(RowIdx, ColumnName);
end;

C++Script, C#Script

function Main1 ()
{
  var p, Grid, Cell, ColumnName, RowIdx;

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

  // Obtain and select the cell
  ColumnName = "FirstName";
  RowIdx = 5; // Note that index is zero-based

  // Simulate a click on a cell
  Grid["ClickCell"](RowIdx, ColumnName);
}

Using the Native Methods and Properties of a UltraGridCell Object

It is possible to select a particular cell using the native methods and properties of a UltraGridCell object. To get an object that corresponds to the desired cell you can use the GetCell routine. This routine returns an object that corresponds to the grid cell specified by the row index and the column index or name. You can find the code for this routine in the Accessing Grid Elements in Infragistics UltraGrid topic.

To select a retrieved cell, you can either call the UltraGridCell.Activate method or assign True to a cell’s Boolean property - UltraGridCell.Activated.

The code below selects a cell via the native properties and methods of the UltraGridCell object. It uses the same grid, form and application as in the first example and selects the same cell.

JavaScript

function Main2 ()
{
  var p, Grid, Cell, ColumnName, RowIdx;

  // Obtain the grid object
  p = Sys.Process("SamplesExplorer");
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");

  // Obtain and select the cell
  ColumnName = "FirstName";
  RowIdx = 5; // Note that index is zero-based
  Cell = GetCell (Grid, null, RowIdx, ColumnName);
  if (strictEqual(Cell, null))
    Log.Error ("Cell (" + RowIdx + ", " + ColumnName + ") was not found.")
  else
  {
    // 1: Using the cell's Activate method
    Cell.Activate();
    // 2: Using the cell's Activated property
    //Cell.Activated = true;
  }
}

JScript

function Main2 ()
{
  var p, Grid, Cell, ColumnName, RowIdx;

  // Obtain the grid object
  p = Sys.Process("SamplesExplorer");
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");

  // Obtain and select the cell
  ColumnName = "FirstName";
  RowIdx = 5; // Note that index is zero-based
  Cell = GetCell (Grid, null, RowIdx, ColumnName);
  if (Cell == null)
    Log.Error ("Cell (" + RowIdx + ", " + ColumnName + ") was not found.")
  else
  {
    // 1: Using the cell's Activate method
    Cell.Activate();
    // 2: Using the cell's Activated property
    //Cell.Activated = true;
  }
}

Python

def Main2 ():

  # Obtain the grid object
  p = Sys.Process("SamplesExplorer")
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")

  # Obtain and select the cell
  ColumnName = "FirstName"
  RowIdx = 5 # Note that index is zero-based
  Cell = GetCell (Grid, None, RowIdx, ColumnName)
  if (Cell == None):
    Log.Error ("Cell (" + RowIdx + ", " + ColumnName + ") was not found.")
  else:
    # 1: Using the cell's Activate method
    Cell.Activate()
    # 2: Using the cell's Activated property
    #Cell.Activated = True

VBScript

Sub Main2
  Dim p, Grid, Cell, ColumnName, RowIdx

  ' Obtain the grid object
  Set p = Sys.Process("SamplesExplorer")
  Set Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")

  ' Obtain and select the cell
  ColumnName = "FirstName"
  RowIdx = 5 ' Note that index is zero-based
  Set Cell = GetCell (Grid, Nothing, RowIdx, ColumnName)
  If Cell Is Nothing Then
    Log.Error ("Cell (" & CStr(RowIdx) & ", " & ColumnName & ") was not found.")
  Else
    ' 1: Using the cell's Activate method
    Cell.Activate
    ' 2: Using the cell's Activated property
    'Cell.Activated = True
  End If
End Sub

DelphiScript

procedure Main2;
var p, Grid, Cell, ColumnName, RowIdx : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('SamplesExplorer');
  Grid := p.WinFormsObject('frmFixedHeaders').WinFormsObject('UltraGrid1');

  ColumnName := 'FirstName';
  RowIdx := 5; // Note that index is zero-based
  Cell := GetCell(Grid, nil, RowIdx, ColumnName);
  if Cell = nil then 
    Log.Error ('Cell (' + aqConvert.IntToStr(RowIdx) + ', ' + ColumnName + ') was not found.')
  else
  begin
    // 1: Using the cell's Activate method
    Cell.Activate;
    // 2: Using the cell's Activated property
    //Cell.Activated := true;
  end
end;

C++Script, C#Script

function Main2 ()
{
  var p, Grid, Cell, ColumnName, RowIdx;

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

  // Obtain and select the cell
  ColumnName = "FirstName";
  RowIdx = 5; // Note that index is zero-based
  Cell = GetCell (Grid, null, RowIdx, ColumnName);
  if (Cell == null)
    Log["Error"]("Cell (" + RowIdx + ", " + ColumnName + ") was not found.")
  else
  {
    // 1: Using the cell's Activate method
    Cell["Activate"]();
    // 2: Using the cell's Activated property
    //Cell["Activated"] = true;
  }
}

Using the UltraGrid.ActiveCell Property

Another way to select a cell is to use the native properties of the UltraGrid control. The UltraGrid object has the UltraGrid.ActiveCell native property that allows you to determine or specify the cell focused in the grid. This property accepts and returns the UltraGridCell object. To obtain an UltraGridCell instance that represents a desired grid cell use the GetCell routine.

The following code demonstrates how to use the UltraGrid.ActiveCell property to select a grid cell.

JavaScript

function Main3 ()
{
  var p, Grid, Cell, ColumnName, RowIdx;

  // Obtain the grid object
  p = Sys.Process("SamplesExplorer");
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");

  // Obtain and select the cell
  ColumnName = "FirstName";
  RowIdx = 5; // Note that index is zero-based
  Cell = GetCell (Grid, null, RowIdx, ColumnName);
  if (strictEqual(Cell, null))
    Log.Error ("Cell (" + RowIdx + ", " + ColumnName + ") was not found.")
  else
    Grid.ActiveCell = Cell;
}

JScript

function Main3 ()
{
  var p, Grid, Cell, ColumnName, RowIdx;

  // Obtain the grid object
  p = Sys.Process("SamplesExplorer");
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");

  // Obtain and select the cell
  ColumnName = "FirstName";
  RowIdx = 5; // Note that index is zero-based
  Cell = GetCell (Grid, null, RowIdx, ColumnName);
  if (Cell == null)
    Log.Error ("Cell (" + RowIdx + ", " + ColumnName + ") was not found.")
  else
    Grid.ActiveCell = Cell;
}

Python

def Main3 ():

  # Obtain the grid object
  p = Sys.Process("SamplesExplorer")
  Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")

  # Obtain and select the cell
  ColumnName = "FirstName"
  RowIdx = 5 # Note that index is zero-based
  Cell = GetCell (Grid, None, RowIdx, ColumnName)
  if (Cell == None):
    Log.Error ("Cell (" + RowIdx + ", " + ColumnName + ") was not found.")
  else:
    Grid.ActiveCell = Cell

VBScript

Sub Main3
  Dim p, Grid, Cell, ColumnName, RowIdx

  ' Obtain the grid object
  Set p = Sys.Process("SamplesExplorer")
  Set Grid = p.WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")

  ' Obtain and select the cell
  ColumnName = "FirstName"
  RowIdx = 5 ' Note that index is zero-based
  Set Cell = GetCell (Grid, Nothing, RowIdx, ColumnName)
  If Cell Is Nothing Then
    Log.Error ("Cell (" & CStr(RowIdx) & ", " & ColumnName & ") was not found.")
  Else
    Set Grid.ActiveCell = Cell
  End If
End Sub

DelphiScript

procedure Main3;
var p, Grid, Cell, ColumnName, RowIdx : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('SamplesExplorer');
  Grid := p.WinFormsObject('frmFixedHeaders').WinFormsObject('UltraGrid1');

  ColumnName := 'FirstName';
  RowIdx := 5; // Note that index is zero-based
  Cell := GetCell(Grid, nil, RowIdx, ColumnName);
  if Cell = nil then 
    Log.Error ('Cell (' + aqConvert.IntToStr(RowIdx) + ', ' + ColumnName + ') was not found.')
  else
    Grid.ActiveCell := Cell;
end;

C++Script, C#Script

function Main3 ()
{
  var p, Grid, Cell, ColumnName, RowIdx;

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

  // Obtain and select the cell
  ColumnName = "FirstName";
  RowIdx = 5; // Note that index is zero-based
  Cell = GetCell (Grid, null, RowIdx, ColumnName);
  if (Cell == null)
    Log["Error"]("Cell (" + RowIdx + ", " + ColumnName + ") was not found.")
  else
    Grid["ActiveCell"] = Cell;
}

See Also

Working With Infragistics UltraGrid
Accessing Grid Elements in Infragistics UltraGrid
Working With Specific In-place Editors in Infragistics UltraGrid
Obtaining and Setting Cell Values in Infragistics UltraGrid
Copying and Pasting Cell Values in Infragistics UltraGrid
Selecting Multiple Rows in Infragistics UltraGrid
ClickCell Action (Grid Controls)
ClickCellR Action (Grid Controls)

Highlight search results