Selecting Cells in Syncfusion GridDataBoundGrid

Applies to TestComplete 15.68, last modified on October 16, 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 GridDataBoundGrid. 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 GridDataBoundGrid rows and columns, see Accessing Rows, Columns and Cells in Syncfusion GridDataBoundGrid.

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

When testing Syncfusion GridDataBoundGrid 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 GridDataBoundGrid 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 GridDataBoundGrid 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 GridDataBoundGrid object, RowIndex and ColIndex are the zero-based absolute indexes of the cell’s row and column. (For more information on identifying GridDataBoundGrid rows and columns, see Accessing Rows, Columns and Cells in Syncfusion GridDataBoundGrid.) The resulting rect object is a .NET System.Drawing.Rectangle 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 GridDataBoundGrid.ScrollCellInView method:

GridObj.ScrollCellInView_3 ( RowIndex, ColIndex )

The following example demonstrates how you can select GridDataBoundGrid 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 ("DataBoundSortByDisplayMember");
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1");

  // 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, ColumnId)
{
  var ColIndex, rect;

  // Convert the column's identifier to its absolute index
  ColIndex = GetColIndexById (Grid, ColumnId);

  // 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);
}

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")

  # 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, ColumnId):

  # Convert the column's identifier to its absolute index
  ColIndex = GetColIndexById (Grid, ColumnId)

  # 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)

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

VBScript

Sub Main
  Dim p, Grid

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

  ' 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, ColumnId)
  Dim ColIndex, rect

  ' Convert the column's identifier to its index
  ColIndex = GetColIndexById (Grid, ColumnId)

  ' 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

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

DelphiScript

procedure ClickCell (Grid, RowIndex, ColumnId); forward;
function GetColIndexById (Grid, ColumnId); forward;

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

  // 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;

procedure ClickCell (Grid, RowIndex, ColumnId);
var ColIndex, rect : OleVariant;
begin
  // Convert the column's identifier to its index
  ColIndex := GetColIndexById (Grid, ColumnId);

  // 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;

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;

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

  // 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, ColumnId)
{
  var ColIndex, rect;

  // Convert the column's identifier to its absolute index
  ColIndex = GetColIndexById (Grid, ColumnId);

  // 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);
}

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

Simulating Keyboard Shortcuts

The GridDataBoundGrid control 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 GridDataBoundGrid.CurrentCell Property

The GridDataBoundGrid control 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 GridDataBoundGrid.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 GridDataBoundGrid.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 ("DataBoundSortByDisplayMember");
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridDataBoundGrid1");

  // 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, ColumnId)
{
  // Try to navigate to the specified cell
  if (Grid.CurrentCell.MoveTo_3 (RowIndex, GetColIndexById(Grid, ColumnId)))
    // Make the selected cell visible
    Grid.CurrentCell.ScrollInView()
  else
    Log.Error ("Cell (" + RowIndex + ", " + ColumnId + ") could not be selected.");
}

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")

  # 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, ColumnId):
  # Try to navigate to the specified cell
  if (Grid.CurrentCell.MoveTo_3 (RowIndex, GetColIndexById(Grid, ColumnId))):
    # Make the selected cell visible
    Grid.CurrentCell.ScrollInView()
  else:
    Log.Error ("Cell (" + RowIndex + ", " + ColumnId + ") could not be selected.")

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

VBScript

Sub Main
  Dim p, Grid

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

  ' 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, ColumnId)
  ' Try to navigate to the specified cell
  If Grid.CurrentCell.MoveTo_3 (RowIndex, GetColIndexById(Grid, ColumnId)) Then
    ' Make the selected cell visible
    Grid.CurrentCell.ScrollInView
  Else
    Call Log.Error ("Cell (" & RowIndex & ", " & ColumnId & ") could not be selected.")
  End If
End Sub

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

DelphiScript

procedure SelectCell (Grid, RowIndex, ColumnId); forward;
function GetColIndexById (Grid, ColumnId); forward;

procedure Main;
var p, Grid : OleVariant;
begin

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

  // 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;

procedure SelectCell (Grid, RowIndex, ColumnId);
begin
  // Try to navigate to the specified cell
  if Grid.CurrentCell.MoveTo_3 (RowIndex, GetColIndexById(Grid, ColumnId)) then
    // Make the selected cell visible
    Grid.CurrentCell.ScrollInView
  else
    Log.Error ('Cell (' + aqConvert.VarToStr(RowIndex) + ', ' + aqConvert.VarToStr(ColumnId) + ') could not be selected.');
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;

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

  // 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, ColumnId)
{
  // Try to navigate to the specified cell
  if (Grid["CurrentCell"]["MoveTo_3"](RowIndex, GetColIndexById(Grid, ColumnId)))
    // Make the selected cell visible
    Grid["CurrentCell"]["ScrollInView"]()
  else
    Log["Error"]("Cell (" + RowIndex + ", " + ColumnId + ") could not be selected.");
}

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
Obtaining and Setting Cell Values in Syncfusion GridDataBoundGrid
Selecting Multiple Rows in Syncfusion GridDataBoundGrid
Searching for Records in Syncfusion GridDataBoundGrid

Highlight search results