Selecting Cells in Developer Express QuantumGrid

Applies to TestComplete 15.63, last modified on April 23, 2024

One of the actions that you will perform the most often over a grid is selecting cells. This topic provides an example that selects cells of the QuantumGrid control.

In order for TestComplete to be able to perform these actions, the following conditions must be met: Note also, that the compiler can exclude methods and properties that are not called in the application’s source code from the application’s binary code, so these methods and properties are unavailable to TestComplete (see Object Properties, Fields and Methods That Are Unavailable to TestComplete). To solve the problem, make sure that the desired methods and properties are used in the application’s source code. For instance, you can add a virtual method to your application that calls the desired methods and properties (the compiler does not exclude virtual methods).

When testing Developer Express QuantumGrid controls, use specific methods and properties of the corresponding DevExpressQuantumGrid 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 QuantumGrid 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

You can select a particular grid cell by simulating a mouse click on that cell. To simulate cell clicks, use the ClickCell, DblClickCell, ClickCellR and similar actions of the DevExpressQuantumGrid object or the DevExpressQuantumGridLevel object that corresponds to a grid’s child level. All of these actions have parameters that specify the grid view, row (card) and column (card field) that contain the desired cell. 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.

Below is an example that demonstrates how you can simulate clicks on grid cells:

JavaScript, JScript

function Main ()
{
  var p, Grid, ColumnName1, ColumnName2, ColumnName3;

  // Obtain the grid object
  p = Sys.Process("MySampleApp");
  Grid = p.VCLObject("FormName").VCLObject("GridName");

  // Click some cells
  ColumnName1 = "MyColumn1";
  ColumnName2 = "MyColumn2";
  ColumnName3 = "MyColumn3";
  Grid.ClickCell (0, ColumnName1);
  Grid.ClickCell (0, ColumnName2);
  Grid.ClickCell (0, ColumnName3);
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process("MySampleApp")
  Grid = p.VCLObject("FormName").VCLObject("GridName")

  # Click some cells
  ColumnName1 = "MyColumn1"
  ColumnName2 = "MyColumn2"
  ColumnName3 = "MyColumn3"
  Grid.ClickCell (0, ColumnName1)
  Grid.ClickCell (0, ColumnName2)
  Grid.ClickCell (0, ColumnName3)

VBScript

Sub Main
  Dim p, Grid, ColumnName1, ColumnName2, ColumnName3

  ' Obtain the grid object
  Set p = Sys.Process("MySampleApp")
  Set Grid = p.VCLObject("FormName").VCLObject("GridName")

  ' Click some cells
  ColumnName1 = "MyColumn1"
  ColumnName2 = "MyColumn2"
  ColumnName3 = "MyColumn3"
  Call Grid.ClickCell (0, ColumnName1)
  Call Grid.ClickCell (0, ColumnName2)
  Call Grid.ClickCell (0, ColumnName3)
End Sub

DelphiScript

procedure Main;
var p, ColumnName1, ColumnName2, ColumnName3, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('MySampleApp');
  Grid := p.VCLObject('FormName').VCLObject('GridName');

  // Click some cells
  ColumnName1 := 'MyColumn1';
  ColumnName2 := 'MyColumn2';
  ColumnName3 := 'MyColumn3';
  Grid.ClickCell (0, ColumnName1);
  Grid.ClickCell (0, ColumnName2);
  Grid.ClickCell (0, ColumnName3);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, ColumnName1, ColumnName2, ColumnName3;

  // Obtain the grid object
  p = Sys["Process"]("MySampleApp");
  Grid = p["VCLObject"]("FormName")["VCLObject"]("GridName");

  // Click some cells
  ColumnName1 = "MyColumn1";
  ColumnName2 = "MyColumn2";
  ColumnName3 = "MyColumn3";
  Grid.ClickCell (0, ColumnName1);
  Grid.ClickCell (0, ColumnName2);
  Grid.ClickCell (0, ColumnName3);
}

Using TcxGrid Internal Properties

You can also select grid cells using internal properties of the TcxGrid object (TcxGrid is the class name of the QuantumGrid control):

  • ViewObj.Controller.FocusedColumn or ViewObj.Controller.FocusedColumnIndex - Using these properties you can move the focus to the specified column or obtain the column object that corresponds to the focused column in a view (the QuantumGrid control can contain several row levels. Each level has a view that defines how the data is displayed. See also Getting Views in Developer Express QuantumGrid).
    The FocusedColumn property returns the column object. The FocusedColumnIndex property returns the index (zero-based) of the focused column.
  • ViewObj.Controller.FocusedRowIndex - Specifies the index of the focused row for a view.
  • GridObj.ActiveView - Returns the “native” view object corresponding to the root level.
In order for you to be able to use these properties, the property code must be included into the application’s binary file. The problem is that Delphi’s (or C++Builder’s) smart linker excludes methods and properties that are not used within the application. So, it is possible that the mentioned properties are not included into the application’s executable, or the application provides limited access to them (for instance, a read/write property can be seen as read-only or write-only).

To solve the problem, you can create a virtual method in your code that will call the needed properties. For example, you can use the following code:

Delphi

interface
...
TForm1 = class(TForm)
 ...
  private
    { Private declarations }
    ...
  public
    { Public declarations }
    ...
    procedure ExposeNeededFunctions; virtual;
  end;
...
implementation
...
procedure TForm1.ExposeNeededFunctions;
begin
  (cxGrid1.ActiveView as TcxGridDBTableView).Controller.FocusedRowIndex := 1;
  (cxGrid1.ActiveView as TcxGridDBTableView).Controller.FocusedColumn := nil;
  // Or
  // (cxGrid1.ActiveView as TcxGridDBTableView).Controller.FocusedColumnIndex := 0;

end;

C++Builder

/* MyUnit.h */

class TForm1 : public TForm
{
__published:    // IDE-managed Components
   ...
private:        // User declarations
   ...
public:         // User declarations
   ...
   virtual void ExposeNeededFunctions();
};


/* MyUnit.cpp */

virtual void TForm1::ExposeNeededFunctions()
{
  ((TcxGridDBTableView) cxGrid1.ActiveView).Controller.FocusedRowIndex = 1;
  ((TcxGridDBTableView) cxGrid1.ActiveView).Controller.FocusedColumn = NULL;
  // Or
  // ((TcxGridDBTableView) cxGrid1.ActiveView).Controller.FocusedColumnIndex = 0;
}

In the example, we typecast the cxGrid1.ActiveView object to the TcxGridDBTableView type. In your application, the type name must coincide with the view data type.

Note that the cell selection is only possible if the grid displays data in cell selection mode. This mode is active, if the view’s OptionsView.CellSelect is set to False. If you are not sure which mode the tested grid functions, you may ask the tested application’s developer. Also, you can determine the mode visually by watching the grid’s behavior when you are navigating through the records. If the grid highlights whole rows rather than individual cells, then the row selection mode is active and you cannot simulate the cell selection.

Example

View description

JavaScript

function Main ()
{
  var p, Grid, ColumnName1, ColumnName2, ColumnName3;

  // Obtain the grid object
  p = Sys.Process("MySampleApp");
  Grid = p.VCLObject("FormName").VCLObject("GridName");

  // Select some cells
  ColumnName1 = "MyColumn1";
  ColumnName2 = "MyColumn2";
  ColumnName3 = "MyColumn3";
  SelectCell (Grid, null, 0, ColumnName1);
  SelectCell (Grid, null, 2, ColumnName2);
  SelectCell (Grid, null, 3, ColumnName3);
}

// Select the specified grid cell
function SelectCell (Grid, View, RowIndex, ColumnId)
{
  // Obtain the view object
  if (strictEqual(View, null))
    View = Grid.ActiveView;
  // Select the cell's row and column
  View.Controller.FocusedRowIndex = RowIndex;
  View.Controller.FocusedColumn = GetColumn (Grid, View, ColumnId);
}

// Obtain the column object
function GetColumn (Grid, View, ColumnId)
{
  // Obtain the view object
  if (strictEqual(View, null))
    View = Grid.ActiveView;

  // Check type of the columnId parameter
  if (equal(aqObject.GetVarType(ColumnId), varOleStr))
  {
    // If ColumnId is a string,
    // then search for the column by its caption
    for (let i = 0; i < View.ColumnCount; i++)
      if (equal(View.Columns(i).Caption, ColumnId))
        return View.Columns(i); // Column id found

    return null; // Column is not found
  }
  else
    // If ColumnId is an integer,
    // then search for column by its index
    return View.Columns(ColumnId);
}

JScript

function Main ()
{
  var p, Grid, ColumnName1, ColumnName2, ColumnName3;

  // Obtain the grid object
  p = Sys.Process("MySampleApp");
  Grid = p.VCLObject("FormName").VCLObject("GridName");

  // Select some cells
  ColumnName1 = "MyColumn1";
  ColumnName2 = "MyColumn2";
  ColumnName3 = "MyColumn3";
  SelectCell (Grid, null, 0, ColumnName1);
  SelectCell (Grid, null, 2, ColumnName2);
  SelectCell (Grid, null, 3, ColumnName3);
}

// Select the specified grid cell
function SelectCell (Grid, View, RowIndex, ColumnId)
{
  // Obtain the view object
  if (View == null)
    View = Grid.ActiveView;
  // Select the cell's row and column
  View.Controller.FocusedRowIndex = RowIndex;
  View.Controller.FocusedColumn = GetColumn (Grid, View, ColumnId);
}

// Obtain the column object
function GetColumn (Grid, View, ColumnId)
{
  // Obtain the view object
  if (View == null)
    View = Grid.ActiveView;

  // Check type of the columnId parameter
  if (aqObject.GetVarType(ColumnId) == varOleStr)
  {
    // If ColumnId is a string,
    // then search for the column by its caption
    for (var i = 0; i < View.ColumnCount; i++)
      if (View.Columns(i).Caption == ColumnId)
        return View.Columns(i); // Column id found

    return null; // Column is not found
  }
  else
    // If ColumnId is an integer,
    // then search for column by its index
    return View.Columns(ColumnId);
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process("MySampleApp")
  Grid = p.VCLObject("FormName").VCLObject("GridName")

  # Select some cells
  ColumnName1 = "MyColumn1"
  ColumnName2 = "MyColumn2"
  ColumnName3 = "MyColumn3"
  SelectCell (Grid, None, 0, ColumnName1)
  SelectCell (Grid, None, 2, ColumnName2)
  SelectCell (Grid, None, 3, ColumnName3)

# Select the specified grid cell 
def SelectCell (Grid, View, RowIndex, ColumnId):
  # Obtain the view object 
  if (View == None):
    View = Grid.ActiveView
  # Select the cell's row and column
  View.Controller.FocusedRowIndex = RowIndex
  View.Controller.FocusedColumn = GetColumn (Grid, View, ColumnId)

# Obtain the column object
def GetColumn (Grid, View, ColumnId):
  # Obtain the view object 
  if (View == None):
    View = Grid.ActiveView

  # Check type of the columnId parameter 
  if (aqObject.GetVarType(ColumnId) == varOleStr):
    # If ColumnId is a string,
    # then search for the column by its caption
    for i in range(0, View.ColumnCount-1):
      if (View.Columns[i].Caption == ColumnId):
        return View.Columns[i] # Column id found

    return None # Column is not found
  else:
    # If ColumnId is an integer,
    # then search for column by its index
    return View.Columns(ColumnId)

VBScript

Sub Main
  Dim p, Grid, ColumnName1, ColumnName2, ColumnName3

  ' Obtain the grid object
  Set p = Sys.Process("MySampleApp")
  Set Grid = p.VCLObject("FormName").VCLObject("GridName")

  ' Select some cells
  ColumnName1 = "MyColumn1"
  ColumnName2 = "MyColumn2"
  ColumnName3 = "MyColumn3"
  Call SelectCell (Grid, Nothing, 0, ColumnName1)
  Call SelectCell (Grid, Nothing, 2, ColumnName2)
  Call SelectCell (Grid, Nothing, 3, ColumnName3)
End Sub

' Select the specified grid cell
Sub SelectCell (Grid, View, RowIndex, ColumnId)
  ' Obtain the view object
  If View Is Nothing Then
    Set View = Grid.ActiveView
  End If
  ' Select the cell's row and column
  View.Controller.FocusedRowIndex = RowIndex
  Set View.Controller.FocusedColumn = GetColumn (Grid, View, ColumnId)
End Sub

' Obtain the column object
Function GetColumn (Grid, View, ColumnId)
  Dim i

  ' Obtain the view object
  If View Is Nothing Then
    Set View = Grid.ActiveView
  End If

  ' Check type of the columnId parameter
  If aqObject.GetVarType(ColumnId) = varOleStr Then
    ' If ColumnId is a string,
    ' then search for the column by its caption
    For i = 0 to View.ColumnCount-1
      If View.Columns(i).Caption = ColumnId Then
        Set GetColumn = View.Columns(i) ' Column id found
        Exit Function
      End If
    Next

    Set GetColumn = Nothing' Column is not found
  Else
    ' If ColumnId is an integer,
    ' then search for column by its index
    Set GetColumn = View.Columns(ColumnId)
  End If
End Function

DelphiScript

procedure SelectCell (Grid, View, RowIndex, ColumnId); forward;
function GetColumn (Grid, View, ColumnId); forward;

procedure Main;
var p, ColumnName1, ColumnName2, ColumnName3, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('MySampleApp');
  Grid := p.VCLObject('FormName').VCLObject('GridName');

  // Select some cells
  ColumnName1 := 'MyColumn1';
  ColumnName2 := 'MyColumn2';
  ColumnName3 := 'MyColumn3';
  SelectCell (Grid, nil, 0, ColumnName1);
  SelectCell (Grid, nil, 2, ColumnName2);
  SelectCell (Grid, nil, 3, ColumnName3);
end;

// Select the specified grid cell
procedure SelectCell (Grid, View, RowIndex, ColumnId);
begin
  // Obtain the view object
  if View = nil then
    View := Grid.ActiveView;
  // Select the cell's row and column
  View.Controller.FocusedRowIndex := RowIndex;
  View.Controller.FocusedColumn := GetColumn (Grid, View, ColumnId);
end;

// Obtain the column object
function GetColumn (Grid, View, ColumnId);
var i : OleVariant;
begin
  // Obtain the view object
  if View = nil then
    View := Grid.ActiveView;

  // Check type of the columnId parameter
  if aqObject.GetVarType(ColumnId) = varOleStr then
  begin
    // If ColumnId is a string,
    // then search for the column by its caption
    for i := 0 to View.ColumnCount-1 do
      if View.Columns[i].Caption = ColumnId then
      begin
        Result := View.Columns[i]; // Column id found
        Exit;
      end;

    Result := nil; // Column is not found
  end
  else
    // If ColumnId is an integer,
    // then search for column by its index
    Result := View.Columns[ColumnId];
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, ColumnName1, ColumnName2, ColumnName3;

  // Obtain the grid object
  p = Sys["Process"]("MySampleApp");
  Grid = p["VCLObject"]("FormName")["VCLObject"]("GridName");

  // Select some cells
  ColumnName1 = "MyColumn1";
  ColumnName2 = "MyColumn2";
  ColumnName3 = "MyColumn3";
  SelectCell (Grid, null, 0, ColumnName1);
  SelectCell (Grid, null, 2, ColumnName2);
  SelectCell (Grid, null, 3, ColumnName3);
}

// Select the specified grid cell
function SelectCell (Grid, View, RowIndex, ColumnId)
{
  // Obtain the view object
  if (View == null)
    View = Grid["ActiveView"];
  // Select the cell's row and column
  View["Controller"]["FocusedRowIndex"] = RowIndex;
  View["Controller"]["FocusedColumn"] = GetColumn (Grid, View, ColumnId);
}

// Obtain the column object
function GetColumn (Grid, View, ColumnId)
{
  // Obtain the view object
  if (View == null)
    View = Grid["ActiveView"];

  // Check type of the columnId parameter
  if (aqObject["GetVarType"](ColumnId) == varOleStr)
  {
    // If ColumnId is a string,
    // then search for the column by its caption
    for (var i = 0; i < View["ColumnCount"]; i++)
      if (View["Columns"](i)["Caption"] == ColumnId)
        return View["Columns"](i); // Column id found

    return null; // Column is not found
  }
  else
    // If ColumnId is an integer,
    // then search for column by its index
    return View["Columns"](ColumnId);
}

See Also

Working With Developer Express QuantumGrid
Activating In-place Editors in Developer Express QuantumGrid
Obtaining and Setting Cell Values in Developer Express QuantumGrid
Selecting Multiple Rows in Developer Express QuantumGrid
ClickCell Action (Specific to DevExpressQuantumGrid Controls)

Highlight search results