Selecting Cells in Syncfusion GridControl

Applies to TestComplete 15.70, last modified on December 17, 2024

One of the actions that you will perform the most over a grid is selecting cells. This topic describes various approaches that can be used to select a particular cell in Syncfusion GridControl. Note that before selecting a cell you should identify the row and column in which the desired cell resides. For example, you can search for the row by the value or text in a particular column. For more information on addressing GridControl rows and columns, see Accessing Rows, Columns and Cells in Syncfusion GridControl.

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

When testing Syncfusion GridControl controls, use specific methods and properties of the corresponding SyncfusionEssGrid 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 GridControl 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 Cell Clicks

You can select a particular GridControl by simulating a mouse click on it. To do this, use the Click action applied to the grid control. This action requires that you know the coordinates of the desired cell relative to the grid. You can determine them using the following instruction:

rect = GridObj.RangeInfoToRectangle ( GridObj.GridCellsRange.Cell( RowIndex, ColIndex ) )

Here, GridObj is the GridControl object, RowIndex and ColIndex are the zero-based absolute indexes of the cell’s row and column. (For more information on identifying GridControl rows and columns, see Accessing Rows, Columns and Cells in Syncfusion GridControl.) The resulting rect object is a .NET System.Drawing.Rectangles object that holds the grid-relative coordinates of the desired cell.

Note that before simulating a click on the cell, you need to make sure that the cell is situated in the grid’s visible area. You can scroll the cell into the visible area using the GridControl.ScrollCellInView method:

GridObj.ScrollCellInView_3 ( RowIndex, ColIndex )

The following example demonstrates how you can select GridControl cells by simulating a mouse click on them.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

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

  // Click the cell in the 11th row and the 3nd column
  ClickCell (Grid, 10, 2);
  aqUtils.Delay (1000);
  // Click the cell in the 21th row and the 5nd column
  ClickCell (Grid, 20, 4);
}

function ClickCell (Grid, RowIndex, ColIndex)
{
  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get the cell coordinates
  var rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
}

Python

def Main ():

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

  # Click the cell in the 11th row and the 3nd column
  ClickCell (Grid, 10, 2)
  aqUtils.Delay (1000)
  # Click the cell in the 21th row and the 5nd column
  ClickCell (Grid, 20, 4)

def ClickCell (Grid, RowIndex, ColIndex):
  # Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex)

  # Get the cell coordinates
  rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex))

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2)

VBScript

Sub Main
  Dim p, Grid

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

  ' Click the cell in the 11th row and the 3nd column
  Call ClickCell (Grid, 10, 2)
  Call aqUtils.Delay (1000)
  ' Click the cell in the 21th row and the 5nd column
  Call ClickCell (Grid, 20, 4)
End Sub

Sub ClickCell (Grid, RowIndex, ColIndex)
  Dim rect

  ' Make cell visible
  Call Grid.ScrollCellInView_3 (RowIndex, ColIndex)

  ' Get the cell coordinates
  Set rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex))

  Call Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2)
End Sub

DelphiScript

procedure ClickCell (Grid, RowIndex, ColIndex);
var rect : OleVariant;
begin
  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get the cell coordinates
  rect := Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
end;

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

  // Click the cell in the 11th row and the 3nd column
  ClickCell (Grid, 10, 2);
  aqUtils.Delay (1000);
  // Click the cell in the 21th row and the 5nd column
  ClickCell (Grid, 20, 4);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

  // Click the cell in the 11th row and the 3nd column
  ClickCell (Grid, 10, 2);
  aqUtils["Delay"] (1000);
  // Click the cell in the 21th row and the 5nd column
  ClickCell (Grid, 20, 4);
}

function ClickCell (Grid, RowIndex, ColIndex)
{
  // Make cell visible
  Grid["ScrollCellInView_3"](RowIndex, ColIndex);

  // Get the cell coordinates
  var rect = Grid["RangeInfoToRectangle"](Grid["GridCellsRange"]["Cell"](RowIndex, ColIndex));

  Grid["Click"](rect["X"] + rect["Width"]/2, rect["Y"] + rect["Height"]/2);
}

Simulating Keyboard Shortcuts

GridControl supports various keyboard shortcuts that can be used to navigate through the grid. For example, pressing an arrow key selects the neighbor cell in the direction of the arrow, Ctrl+Home navigates to the grid’s upper-left cell, Ctrl+End navigates to the bottom-right cell, and so on.

To simulate a keyboard shortcut, use the Keys action applied to the grid control. The code snippet below demonstrates how to simulate the Ctrl+Home shortcut in order to select the grid’s upper left cell:

JavaScript, JScript

Grid.Keys ("^[Home]");

Python

Grid.Keys ("^[Home]")

VBScript

Call Grid.Keys ("^[Home]")

DelphiScript

Grid.Keys ('^[Home]');

C++Script, C#Script

Grid["Keys"]("^[Home]");

Using the GridControl.CurrentCell Property

The GridControl object has the CurrentCell property that provides access to the currently selected grid cell and lets you move the cell’s focus. To select a new grid cell, you can use various overload versions of the GridControl.CurrentCell.MoveTo method. For example, the MoveTo_3(RowIndex, ColIndex) method will select the cell specified by the absolute row and column indexes. For more information on other MoveTo method versions, refer to the Essential Grid documentation.

Below is an example that demonstrates how you can use the GridControl.CurrentCell.MoveTo_3 method in scripts.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

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

  // Select the cell in the 11th row and the 3nd column
  SelectCell (Grid, 10, 2);
  aqUtils.Delay (1000);
  // Select the cell in the 21th row and the 5nd column
  SelectCell (Grid, 20, 4);
}

function SelectCell (Grid, RowIndex, ColIndex)
{
  // Try to navigate to the specified cell
  if (Grid.CurrentCell.MoveTo_3 (RowIndex, ColIndex))
    // Make the selected cell visible
    Grid.CurrentCell.ScrollInView()
  else
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") could not be selected.");
}

Python

def Main ():

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

  # Select the cell in the 11th row and the 3nd column
  SelectCell (Grid, 10, 2)
  aqUtils.Delay (1000)
  # Select the cell in the 21th row and the 5nd column
  SelectCell (Grid, 20, 4)

def SelectCell (Grid, RowIndex, ColIndex):
  # Try to navigate to the specified cell
  if (Grid.CurrentCell.MoveTo_3 (RowIndex, ColIndex)):
    # Make the selected cell visible
    Grid.CurrentCell.ScrollInView()
  else:
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") could not be selected.")

VBScript

Sub Main
  Dim p, Grid

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

  ' Select the cell in the 11th row and the 3nd column
  Call SelectCell (Grid, 10, 2)
  Call aqUtils.Delay (1000)
  ' Select the cell in the 21th row and the 5nd column
  Call SelectCell (Grid, 20, 4)
End Sub

Sub SelectCell (Grid, RowIndex, ColIndex)
  ' Try to navigate to the specified cell
  If Grid.CurrentCell.MoveTo_3 (RowIndex, ColIndex) Then
    ' Make the selected cell visible
    Grid.CurrentCell.ScrollInView
  Else
    Call Log.Error ("Cell (" & RowIndex & ", " & ColIndex & ") could not be selected.")
  End If
End Sub

DelphiScript

procedure SelectCell (Grid, RowIndex, ColIndex);
begin
  // Try to navigate to the specified cell
  if Grid.CurrentCell.MoveTo_3 (RowIndex, ColIndex) then
    // Make the selected cell visible
    Grid.CurrentCell.ScrollInView
  else
    Log.Error ('Cell (' + aqConvert.VarToStr(RowIndex) + ', ' + aqConvert.VarToStr(ColIndex) + ') could not be selected.');
end;

procedure Main;
var p, Grid : OleVariant;
begin

  // Obtain the application process and the grid object
  p := Sys.Process('GridControlSort');
  Grid := p.WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Select the cell in the 11th row and the 3nd column
  SelectCell (Grid, 10, 2);
  aqUtils.Delay (1000);
  // Select the cell in the 21th row and the 5nd column
  SelectCell (Grid, 20, 4);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

  // Select the cell in the 11th row and the 3nd column
  SelectCell (Grid, 10, 2);
  aqUtils["Delay"] (1000);
  // Select the cell in the 21th row and the 5nd column
  SelectCell (Grid, 20, 4);
}

function SelectCell (Grid, RowIndex, ColIndex)
{
  // Try to navigate to the specified cell
  if (Grid["CurrentCell"]["MoveTo_3"](RowIndex, ColIndex))
    // Make the selected cell visible
    Grid["CurrentCell"]["ScrollInView"]()
  else
    Log["Error"]("Cell (" + RowIndex + ", " + ColIndex + ") could not be selected.");
}

See Also

Working With Syncfusion GridControl
Accessing Rows, Columns and Cells in Syncfusion GridControl
Obtaining and Setting Cell Values in Syncfusion GridControl
Selecting Multiple Rows in Syncfusion GridControl
Searching for Records in Syncfusion GridControl

Highlight search results