Copying and Pasting Cell Values in Borland TDBGrid

Applies to TestComplete 14.92, last modified on September 16, 2021

Copying and pasting cell values are typlical operations that you will perform over grid controls and this topic contains examples that show you how to copy and paste cell values in the TDBGrid control.

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.

Copying Cell Values

To be able to copy a grid cell value, you need to locate the desired cell within the grid, select it and activate its in-place editor. After the in-place editor is activated, you can copy the cell value by simulating the Ctrl+C shortcut or by displaying the context menu and selecting Copy from it. The approach that uses the shortcut is a lot simpler. If you decide to use the context menu item, you will have to calculate the cell coordinates. You can find an example of calculating cell coordinates in the Activating In-place Editors in Borland TDBGrid topic.

Below is code that demonstrates how to copy the cell contents by simulating the Ctrl+C shortcut.

Note: After activating the in-place editor, the cell’s data is selected, so, there is no need to simulate user actions to select the cell’s content. However, if the cell’s content is not selected for some reason, you will have to select it. You can do this by sending the Ctrl+A shortcut to the grid’s window (that is, GridWndObject.Keys("^A")).
Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid, s;

  // Obtain the grid object
  p = Sys.Process("csdemos");
  p.VCLObject("FrmLauncher").VCLObject("BtnViews").ClickButton();
  Grid = p.VCLObject("FrmViewDemo").VCLObject("Panel2").VCLObject("DBGrid1");

  // Copy the cell value
  s = CopyCell (Grid, 4, "LastName");

  // Post the clipboard contents to the log
  Log.Message (s);
}

// Copy the contents of the cell specified by its indexes (both zero-based)
function CopyCell (GridObject, RowIndex, ColumnId)
{
  ActivateCellEditor (GridObject, RowIndex, ColumnId);
  GridObject.Keys ("^c");
  return Sys.Clipboard;
}

// Activate the in-place editor for the specified cell
function ActivateCellEditor (Grid, RowIndex, ColumnId)
{
  Grid.ClickCell (RowIndex, ColumnId);
  Grid.Keys ("[Enter]");
}

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

  # Copy the cell value
  s = CopyCell (Grid, 4, "LastName")

  # Post the clipboard contents to the log
  Log.Message (s)

# Copy the contents of the cell specified by its indexes (both zero-based)
def CopyCell (GridObject, RowIndex, ColumnId):
  ActivateCellEditor (GridObject, RowIndex, ColumnId)
  GridObject.Keys ("^c")
  return Sys.Clipboard

# Activate the in-place editor for the specified cell 
def ActivateCellEditor (Grid, RowIndex, ColumnId):
  Grid.ClickCell (RowIndex, ColumnId)
  Grid.Keys ("[Enter]")

VBScript

Sub Main
  Dim p, Grid, s

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

  ' Copy the cell value
  s = CopyCell (Grid, 4, "LastName")

  ' Post the clipboard contents to the log
  Log.Message (s)
End Sub

' Copy the contents of the cell specified by its indexes (both zero-based)
Function CopyCell (GridObject, RowIndex, ColumnId)
  Call ActivateCellEditor (GridObject, RowIndex, ColumnId)
  GridObject.Keys ("^c")
  CopyCell = Sys.Clipboard
End Function

' Activate the in-place editor for the specified cell
Sub ActivateCellEditor (Grid, RowIndex, ColumnId)
  Call Grid.ClickCell (RowIndex, ColumnId)
  Grid.Keys ("[Enter]")
End Sub

DelphiScript

// Activate the in-place editor for the specified cell
procedure ActivateCellEditor (Grid, RowIndex, ColumnId);
begin
  Grid.ClickCell (RowIndex, ColumnId);
  Grid.Keys ('[Enter]');
end;

// Copy the contents of the cell specified by its indexes (both zero-based)
function CopyCell (GridObject, RowIndex, ColumnId);
begin
  ActivateCellEditor (GridObject, RowIndex, ColumnId);
  GridObject.Keys ('^c');
  Result := Sys.Clipboard;
end;

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

  // Copy the cell value
  s := CopyCell (Grid, 4, 'LastName');

  // Post the clipboard contents to the log
  Log.Message (s);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, s;

  // Obtain the grid object
  p = Sys["Process"]("csdemos");
  p["VCLObject"]("FrmLauncher")["VCLObject"]("BtnViews")["ClickButton"]();
  Grid = p["VCLObject"]("FrmViewDemo")["VCLObject"]("Panel2")["VCLObject"]("DBGrid1");

  // Copy the cell value
  s = CopyCell (Grid, 4, "LastName");

  // Post the clipboard contents to the log
  Log["Message"](s);
}

// Copy the contents of the cell specified by its indexes (both zero-based)
function CopyCell (GridObject, RowIndex, ColumnId)
{
  ActivateCellEditor (GridObject, RowIndex, ColumnId);
  GridObject["Keys"]("^c");
  return Sys["Clipboard"];
}

// Activate the in-place editor for the specified cell
function ActivateCellEditor (Grid, RowIndex, ColumnId)
{
  Grid["ClickCell"](RowIndex, ColumnId);
  Grid["Keys"]("[Enter]");
}

Pasting Cell Values

The paste operation is very similar to the copying operation. To paste a value to a cell, you also need to locate the desired cell within the grid, select it and activate its in-place editor. When the in-place editor is active, you can paste the cell value by simulating the Ctrl+V shortcut or by displaying the context menu and selecting Paste from it. However, the approach that uses the context menu’s items is more complex, since you will have to calculate the coordinates of the right-click. The sample code below demonstrates the Ctrl+V shortcut.

Note that after activating the in-place editor, the whole cell’s data is selected, so there is no need to simulate user actions for selecting the cell’s contents. If the data is not selected for some reason, then the pasting operation will insert the clipboard contents into the cell, but will not replace the cell’s data with the clipboard data. To replace the data, the whole text should be selected in the cell. You can do this by sending the Ctrl+A shortcut to the grid window (that is, GridWndObject.Keys("^A")).

Example

View description

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

  // Set the clipboard contents
  Sys.Clipboard = "Clemens";
  // Paste the clipboard contents to the cell
  PasteCell (Grid, 2, "LastName");
}

// Paste the clipboard contents to the specified cell
function PasteCell (GridObject, RowIndex, ColumnId)
{
  ActivateCellEditor (GridObject, RowIndex, ColumnId);
  GridObject.Keys ("^v" + "[Enter]");
}

// Activate the in-place editor for the specified cell
function ActivateCellEditor (Grid, RowIndex, ColumnId)
{
  Grid.ClickCell (RowIndex, ColumnId);
  Grid.Keys ("[Enter]");
}

Python

def Main2():

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

  # Set the clipboard contents
  Sys.Clipboard = "Clemens"
  # Paste the clipboard contents to the cell
  PasteCell (Grid, 2, "LastName")

# Paste the clipboard contents to the specified cell 
def PasteCell (GridObject, RowIndex, ColumnId):
  ActivateCellEditor (GridObject, RowIndex, ColumnId)
  GridObject.Keys ("^v" + "[Enter]")

# Activate the in-place editor for the specified cell 
def ActivateCellEditor (Grid, RowIndex, ColumnId):
  Grid.ClickCell (RowIndex, ColumnId)
  Grid.Keys ("[Enter]")

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

  ' Set the clipboard contents
  Sys.Clipboard = "Clemens"
  ' Paste the clipboard contents to the cell
  Call PasteCell (Grid, 2, "LastName")
End Sub

' Paste the clipboard contents to the specified cell
Sub PasteCell (GridObject, RowIndex, ColumnId)
  Call ActivateCellEditor (GridObject, RowIndex, ColumnId)
  GridObject.Keys ("^v" & "[Enter]")
End Sub

' Activate the in-place editor for the specified cell
Sub ActivateCellEditor (Grid, RowIndex, ColumnId)
  Call Grid.ClickCell (RowIndex, ColumnId)
  Grid.Keys ("[Enter]")
End Sub

DelphiScript

// Activate the in-place editor for the specified cell
procedure ActivateCellEditor (Grid, RowIndex, ColumnId);
begin
  Grid.ClickCell (RowIndex, ColumnId);
  Grid.Keys ('[Enter]');
end;

// Paste the clipboard contents to the specified cell
procedure PasteCell (GridObject, RowIndex, ColumnId);
begin
  ActivateCellEditor (GridObject, RowIndex, ColumnId);
  GridObject.Keys ('^v' + '[Enter]');
end;

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');

  // Set the clipboard contents
  Sys.Clipboard := 'Clemens';
  // Paste the clipboard contents to the cell
  PasteCell (Grid, 2, 'LastName');
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 = p["VCLObject"]("FrmViewDemo")["VCLObject"]("Panel2")["VCLObject"]("DBGrid1");

  // Set the clipboard contents
  Sys["Clipboard"] = "Clemens";
  // Paste the clipboard contents to the cell
  PasteCell (Grid, 2, "LastName");
}

// Paste the clipboard contents to the specified cell
function PasteCell (GridObject, RowIndex, ColumnId)
{
  ActivateCellEditor (GridObject, RowIndex, ColumnId);
  GridObject["Keys"]("^v" + "[Enter]");
}

// Activate the in-place editor for the specified cell
function ActivateCellEditor (Grid, RowIndex, ColumnId)
{
  Grid["ClickCell"](RowIndex, ColumnId);
  Grid["Keys"]("[Enter]");
}

See Also

Working With Borland TDBGrid
Obtaining and Setting Cell Values in Borland TDBGrid
Selecting Cells in Borland TDBGrid
Activating In-place Editors in Borland TDBGrid
Clipboard Property

Highlight search results