Selecting Cells in Borland TDBGrid

Applies to TestComplete 15.47, last modified on January 20, 2023

One of the actions that you will perform the most over a grid is selecting cells. This topic explains how you can select TDBGrid control’s cells from scripts:

To perform these actions, TestComplete must have access to internal methods and properties of the TDBGrid control. This requires the following conditions be met:

When testing Borland TDBGrid controls, use specific methods and properties of the corresponding BorlandTDBGrid 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 TDBGrid 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.

General Notes

The cell selection is only possible if the grid does not work in row selection mode. The row selection mode is active, if the grid’s Options property contains the dgRowSelect value (this is an internal property of the TDBGrid control). If you are not sure in 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.

Before selecting a cell, you need to locate the desired record within the grid. For example, you can search for and activate the desired row using methods described here: Searching for Records in Borland TDBGrid. After you have determined the cell position in the grid, you can select the cell in one of the following ways:

  • By simulating a mouse click on the cell using the BorlandTDBGrid.ClickCell action. In this case, the cell is automatically put into edit mode so you can modify its value.

  • By focusing the row and column in which the desired cell resides using internal properties and methods of the TDBGrid control.

  • By simulating keyboard shortcuts (for example, Arrow keys).

All of these approaches work equally well. However, in case of using the Col property, TestComplete does not activate the form where the grid resides (if you decide to simulate keystrokes or a click, the grid’s form will be activated). You can use the approach that better suites your needs.

Simulating Clicks on Cells

You can simulate clicks on TDBGrid cells using the ClickCell, ClickCellR, DblClickCell and similar actions of the BorlandTDBGrid object. All of these actions have parameters that specify the row and column 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;
  // Obtain the grid object
  p = Sys.Process("csdemos");
  p.VCLObject("FrmLauncher").VCLObject("BtnViews").ClickButton;
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1");

  Grid.ClickCell(0, "EmpNo");
  Grid.ClickCell(0, "LastName");
  Grid.ClickCell(0, "FirstName");
}

Python

def Main():
  # Obtain the grid object
  p = Sys.Process("csdemos")
  p.VCLObject("FrmLauncher").VCLObject("BtnViews").ClickButton
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")

  Grid.ClickCell(0, "EmpNo")
  Grid.ClickCell(0, "LastName")
  Grid.ClickCell(0, "FirstName")

VBScript

Sub Main
  Dim p, Grid
  ' Obtain the grid object
  Set p = Sys.Process("csdemos")
  p.VCLObject("FrmLauncher").VCLObject("BtnViews").ClickButton
  Set Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")

  Call Grid.ClickCell(0, "EmpNo")
  Call Grid.ClickCell(0, "LastName")
  Call Grid.ClickCell(0, "FirstName")
End Sub

DelphiScript

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('csdemos');
  p.VCLObject('FrmLauncher').VCLObject('BtnViews').ClickButton;
  Grid := p.VCLObject('FrmViewDemo').VCLObject('Panel2').VCLObject('DBGrid1');

  Grid.ClickCell(0, 'EmpNo');
  Grid.ClickCell(0, 'LastName');
  Grid.ClickCell(0, 'FirstName');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;
  // Obtain the grid object
  p = Sys["Process"]("csdemos");
  p["VCLObject"]("FrmLauncher")["VCLObject"]("BtnViews")["ClickButton"];

  Grid["ClickCell"](0, "EmpNo");
  Grid["ClickCell"](0, "LastName");
  Grid["ClickCell"](0, "FirstName");
}

Using the TDBGrid.Col Property

The Col property is only available if the tested application is compiled with debug information. This property is exposed by the Debug Info Agent™. It specifies the currently focused cell in the active row of the grid.

Below is a code snippet that demonstrates how you can select a cell using the Col property of the TDBGrid control:

Example

View description

JavaScript, JScript

function Main()
{
  var p, Grid;
  p = Sys.Process("csdemos");
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1");
  
  SelectCell(Grid, 9, 1);
}

// Activate a row by its index (zero-based)
function ActivateRow(GridObject, RowIndex)
{
  GridObject.DataSource.DataSet.First();
  GridObject.DataSource.DataSet.MoveBy(RowIndex);
}

// Select a call by its indexes (zero-based)
function SelectCell(GridObject, RowIndex, ColIndex)
{
  ActivateRow(GridObject, RowIndex);
  GridObject.Col = ColIndex + GridObject.IndicatorOffset;
}

Python

def Main2():
  p = Sys.Process("csdemos")
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")
  
  SelectCell(Grid, 9, 1)

# Activate a row by its index (zero-based)
def ActivateRow(GridObject, RowIndex):
  GridObject.DataSource.DataSet.First()
  GridObject.DataSource.DataSet.MoveBy(RowIndex)

# Select a call by its indexes (zero-based)
def SelectCell(GridObject, RowIndex, ColIndex):
  ActivateRow(GridObject, RowIndex)
  GridObject.Col = ColIndex + GridObject.IndicatorOffset

VBScript

Sub Main
  Set p = Sys.Process("csdemos")
  Set Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")
  Call SelectCell(Grid, 9, 1)
End Sub

' Selecting a call by its indexes (zero-based)
Sub SelectCell(GridObject, RowIndex, ColIndex)
  Call ActivateRow(GridObject, RowIndex)
  GridObject.Col = ColIndex + GridObject.IndicatorOffset
End Sub

' Activates a row by its index (zero-based)
Sub ActivateRow(GridObject, RowIndex)
  Call GridObject.DataSource.DataSet.First
  Call GridObject.DataSource.DataSet.MoveBy(RowIndex)
End Sub

DelphiScript

// Activate a row by its index (zero-based)
procedure ActivateRow(GridObject, RowIndex);
begin
  GridObject.DataSource.DataSet.First();
  GridObject.DataSource.DataSet.MoveBy(RowIndex);
end;

// Select a call by its indexes (zero-based)
procedure SelectCell(GridObject, Row, Col);
begin
  ActivateRow(GridObject, Row);
  GridObject.Col := Col + GridObject.IndicatorOffset;
end;

procedure Main;
var 
  p, Grid : OleVariant;
begin
  p := Sys.Process('csdemos');
  Grid := p.VCLObject('FrmViewDemo').VCLObject('Panel2').VCLObject('DBGrid1');
  
  SelectCell(Grid, 9, 1);
end;

C++Script, C#Script

function Main()
{
  var p, Grid;
  p = Sys["Process"]("csdemos");
  Grid = p["VCLObject"]("FrmViewDemo")["VCLObject"]("Panel2")["VCLObject"]("DBGrid1");
  
  SelectCell(Grid, 9, 1);
}

// Activate a row by its index (zero-based)
function ActivateRow(GridObject, RowIndex)
{
  GridObject["DataSource"]["DataSet"]["First"]();
  GridObject["DataSource"]["DataSet"]["MoveBy"](RowIndex);
}

// Select a call by its indexes (zero-based)
function SelectCell(GridObject, RowIndex, ColIndex)
{
  ActivateRow(GridObject, RowIndex);
  GridObject["Col"] = ColIndex + GridObject["IndicatorOffset"];
}

Simulating Keyboard Shortcuts

You can select a grid cell by simulating keyboard shortcuts over the grid control. For instance, you can simulate the Home shortcut to move the focus to the first cell in a row, and then simulate a number of Right Arrow key presses to move the selection to the desired cell. The following code demonstrates how you can select a cell by simulating shortcuts.

Example

View description

JavaScript, JScript

function Main()
{
  var p, Grid;
  
  // Obtain the grid object
  p = Sys.Process("csdemos");
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1");
  
  // Activate the inplace editor
  SelectCellByKeystrokes(Grid, 4, 1);
}

// Select a cell with the mouse click
function SelectCellByKeystrokes(GridObject, RowIndex, ColIndex)
{
  var j;

  // Activate the desired row
  ActivateRow(GridObject, RowIndex);
  
  // Lock the event messages
  Log.LockEvents();
  
  // Move focus to the first column
  GridObject.Keys("[Home]");
  // Change the desired column index according the indicator's visibility
  ColIndex = ColIndex + GridObject.IndicatorOffset;

  // Iterate through columns
  for(j = GridObject.IndicatorOffset; j < ColIndex; j++)
  {
    // Move the focus to the next column
    GridObject.Keys("[Right]");
  }
  
  // Unlock the event messages
  Log.UnlockEvents();
}

// Activate a row by its index (zero-based)
function ActivateRow(GridObject, RowIndex)
{
  GridObject.DataSource.DataSet.First();
  GridObject.DataSource.DataSet.MoveBy(RowIndex);
}

Python

def Main3():
  
  # Obtain the grid object
  p = Sys.Process("csdemos")
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")
  
  # Activate the inplace editor
  SelectCellByKeystrokes(Grid, 4, 1)

# Select a cell with the mouse click 
def SelectCellByKeystrokes(GridObject, RowIndex, ColIndex):

  # Activate the desired row
  ActivateRow(GridObject, RowIndex)
  
  # Lock the event messages
  Log.LockEvents()
  
  # Move focus to the first column
  GridObject.Keys("[Home]")
  # Change the desired column index according the indicator's visibility
  ColIndex = ColIndex + GridObject.IndicatorOffset

  # Iterate through columns
  for j in range(GridObject.IndicatorOffset, ColIndex-1):
    # Move the focus to the next column 
    GridObject.Keys("[Right]")
  
  # Unlock the event messages
  Log.UnlockEvents()

# Activate a row by its index (zero-based)
def ActivateRow(GridObject, RowIndex):
  GridObject.DataSource.DataSet.First()
  GridObject.DataSource.DataSet.MoveBy(RowIndex)

VBScript

Sub Main
  ' Obtaining the grid object
  Set p = Sys.Process("csdemos")
  Set Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1")
  
  ' Activating the inplace editor
  Call SelectCellByKeystrokes(Grid, 4, 1)
End Sub

' Selecting a cell with the mouse click
Sub SelectCellByKeystrokes(GridObject, RowIndex, ColIndex)
  ' Activates the desired row
  Call ActivateRow(GridObject, RowIndex)
 
  ' Locking the event messages
  Log.LockEvents
  
  ' Moves focus to the first column
  GridObject.Keys "[Home]"
  ' Changes the desired column index according the indicator's visibility
  ColIndex = ColIndex + GridObject.IndicatorOffset

  ' Iterates through columns
  For j = GridObject.IndicatorOffset To ColIndex - 1
    ' Moves the focus to the next column
    GridObject.Keys "[Right]"
  Next
  
  ' Unlocking the event messages
  Log.UnlockEvents
End Sub

' Activates a row by its index (zero-based)
Sub ActivateRow(GridObject, RowIndex)
  Call GridObject.DataSource.DataSet.First()
  Call GridObject.DataSource.DataSet.MoveBy(RowIndex)
End Sub

DelphiScript

// Activate a row by its index (zero-based)
procedure ActivateRow(GridObject, RowIndex);
begin
  GridObject.DataSource.DataSet.First();
  GridObject.DataSource.DataSet.MoveBy(RowIndex);
end;

// Select a cell with the mouse click
function SelectCellByKeystrokes(GridObject, RowIndex, ColIndex);
var 
  j : OleVariant;
begin
  // Activate the desired row
  ActivateRow(GridObject, RowIndex);
 
  // Lock the event messages
  Log.LockEvents();
  
  // Move focus to the first column
  GridObject.Keys('[Home]');
  
  // Change the desired column index according the indicator's visibility
  ColIndex := ColIndex + GridObject.IndicatorOffset;

  // Iterate through columns
  for j := GridObject.IndicatorOffset to ColIndex - 1 do
  begin
    // Move the focus to the next column
    GridObject.Keys('[Right]');
  end;
  
  // Unlock the event messages
  Log.UnlockEvents();
end;

procedure Main;
var
  p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('csdemos');
  Grid := p.VCLObject('FrmViewDemo').VCLObject('Panel2').VCLObject('DBGrid1');
  
  // Activate the inplace editor
  SelectCellByKeystrokes(Grid, 4, 1);
end;

C++Script, C#Script

function Main()
{
  var p, Grid;
  
  // Obtain the grid object
  p = Sys["Process"]("csdemos");
  Grid = p["VCLObject"]("FrmViewDemo")["VCLObject"]("Panel2")["VCLObject"]("DBGrid1");
  
  // Activate the inplace editor
  SelectCellByKeystrokes(Grid, 4, 1);
}

// Select a cell with the mouse click
function SelectCellByKeystrokes(GridObject, RowIndex, ColIndex)
{
  var j;

  // Activate the desired row
  ActivateRow(GridObject, RowIndex);
  
  // Lock the event messages
  Log["LockEvents"]();
  
  // Move focus to the first column
  GridObject["Keys"]("[Home]");
  // Change the desired column index according the indicator's visibility
  ColIndex = ColIndex + GridObject["IndicatorOffset"];

  // Iterate through columns
  for(j = GridObject["IndicatorOffset"]; j < ColIndex; j++)
  {
    // Move the focus to the next column
    GridObject["Keys"]("[Right]");
  }
  
  // Unlock the event messages
  Log["UnlockEvents"]();
}

// Activate a row by its index (zero-based)
function ActivateRow(GridObject, RowIndex)
{
  GridObject["DataSource"]["DataSet"]["First"]();
  GridObject["DataSource"]["DataSet"]["MoveBy"](RowIndex);
}

See Also

Working With Borland TDBGrid
ClickCell Action (Grid Controls)
Activating In-place Editors in Borland TDBGrid
Obtaining and Setting Cell Values in Borland TDBGrid
Selecting Multiple Rows in Borland TDBGrid

Highlight search results