Copying and Pasting Cell Values in Developer Express QuantumGrid

Applies to TestComplete 15.45, last modified on December 01, 2022

Copying and pasting cell values are typical operations that you will perform over grid controls and this topic contains an example that shows you how to copy and paste cell values in 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.

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 following code demonstrates both approaches. To invoke the context menu and simulate the selection of its Copy item, we simulate pressing the Application and C keys (C is a hot key for the context menu’s Copy item).

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 keystroke to the grid’s window (that is, GridObj.Keys("^A")).
Example

View description

JavaScript

function Main ()
{
  var p, Grid, Level, ColumnName, ViewName;

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

  // Obtain a child level
  Level = Grid.wChildLevel(2);

  // Copy cell value
  ColumnName = "MyColumn";
  ViewName = "MyView";
  CopyCellValue (Grid, Level, 4, ColumnName, ViewName);

  Log.Message ("The clipboard now contains : " + Sys.Clipboard);
}

// Copy the cell value
function CopyCellValue (Grid, Level, RowIndex, ColumnId, ViewId)
{
  // Select the cell
  if (strictEqual(Level, null))
    Grid.ClickCell (RowIndex, ColumnId, ViewId)
  else
    Level.ClickCell (RowIndex, ColumnId, ViewId);

  // Simulate the F2 keystroke to activate the cell's in-place editor
  Grid.Keys ("[F2]");
  
  // Simulate the Ctrl-C keystroke
  Grid.Keys ("^c");
  // To copy the cell contents with the context menu,
  // you can use the following code:
  // Grid.Keys ("[Apps]c");
}

JScript

function Main ()
{
  var p, Grid, Level, ColumnName, ViewName;

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

  // Obtain a child level
  Level = Grid.wChildLevel(2);

  // Copy cell value
  ColumnName = "MyColumn";
  ViewName = "MyView";
  CopyCellValue (Grid, Level, 4, ColumnName, ViewName);

  Log.Message ("The clipboard now contains : " + Sys.Clipboard);
}

// Copy the cell value
function CopyCellValue (Grid, Level, RowIndex, ColumnId, ViewId)
{
  // Select the cell
  if (Level == null)
    Grid.ClickCell (RowIndex, ColumnId, ViewId)
  else
    Level.ClickCell (RowIndex, ColumnId, ViewId);

  // Simulate the F2 keystroke to activate the cell's in-place editor
  Grid.Keys ("[F2]");
  
  // Simulate the Ctrl-C keystroke
  Grid.Keys ("^c");
  // To copy the cell contents with the context menu,
  // you can use the following code:
  // Grid.Keys ("[Apps]c");
}

Python

def Main ():

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

  # Obtain a child level
  Level = Grid.wChildLevel(2)

  # Copy cell value
  ColumnName = "MyColumn"
  ViewName = "MyView"
  CopyCellValue (Grid, Level, 4, ColumnName, ViewName)

  Log.Message ("The clipboard now contains : " + Sys.Clipboard)

# Copy the cell value
def CopyCellValue (Grid, Level, RowIndex, ColumnId, ViewId):
  # Select the cell
  if (Level == None): 
    Grid.ClickCell (RowIndex, ColumnId, ViewId)
  else:
    Level.ClickCell (RowIndex, ColumnId, ViewId)

  # Simulate the F2 keystroke to activate the cell's in-place editor 
  Grid.Keys ("[F2]")
  
  # Simulate the Ctrl-C keystroke
  Grid.Keys ("^c")
  # To copy the cell contents with the context menu, 
  # you can use the following code:
  # Grid.Keys ("[Apps]c")

VBScript

Sub Main
  Dim p, Grid, Level, ColumnName, ViewName

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

  ' Obtain a child level
  Set Level = Grid.wChildLevel(2)
  
  ' Copy cell value
  ColumnName = "MyColumn"
  ViewName = "MyView"
  Call CopyCellValue (Grid, Level, 4, ColumnName, ViewName)

  Log.Message ("The clipboard now contains : " & Sys.Clipboard)
End Sub

' Copy the cell value
Sub CopyCellValue (Grid, Level, RowIndex, ColumnId, ViewId)
  ' Select the cell
  If Level Is Nothing Then 
    Call Grid.ClickCell (RowIndex, ColumnId, ViewId)
  Else
    Call Level.ClickCell (RowIndex, ColumnId, ViewId)
  End If

  ' Simulate the F2 keystroke to activate the cell's in-place editor
  Grid.Keys ("[F2]")
  
  ' Simulate the Ctrl-C keystroke
  Grid.Keys ("^c")
  ' To copy the cell contents with the context menu,
  ' you can use the following code:
  ' Grid.Keys ("[Apps]c")
End Sub

DelphiScript

// Copy the cell value
procedure CopyCellValue (Grid, Level, RowIndex, ColumnId, ViewId);
begin
  // Select the cell
  if Level = nil then 
    Grid.ClickCell (RowIndex, ColumnId, ViewId)
  else
    Level.ClickCell (RowIndex, ColumnId, ViewId);

  // Simulate the F2 keystroke to activate the cell's in-place editor
  Grid.Keys ('[F2]');
  
  // Simulate the Ctrl-C keystroke
  Grid.Keys ('^c');
  // To copy the cell contents with the context menu,
  // you can use the following code:
  // Grid.Keys ('[Apps]c');
end;

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

  // Obtain a child level
  Level := Grid.wChildLevel[2];

  // Copy cell value
  ColumnName := 'MyColumn';
  ViewName := 'MyView';
  CopyCellValue (Grid, Level, 4, ColumnName, ViewName);

  Log.Message ('The clipboard now contains : ' + Sys.Clipboard);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, Level, ColumnName, ViewName;

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

  // Obtain a child level
  Level = Grid["wChildLevel"](2);

  // Copy cell value
  ColumnName = "MyColumn";
  ViewName = "MyView";
  CopyCellValue (Grid, Level, 4, ColumnName, ViewName);

  Log["Message"]("The clipboard now contains : " + Sys["Clipboard"]);
}

// Copy the cell value
function CopyCellValue (Grid, Level, RowIndex, ColumnId, ViewId)
{
  // Select the cell
  if (Level == null)
    Grid["ClickCell"](RowIndex, ColumnId, ViewId)
  else
    Level["ClickCell"](RowIndex, ColumnId, ViewId);

  // Simulate the F2 keystroke to activate the cell's in-place editor
  Grid["Keys"]("[F2]");

  // Simulate the Ctrl-C keystroke
  Grid["Keys"]("^c");
  // To copy the cell contents with the context menu,
  // you can use the following code:
  // Grid["Keys"]("[Apps]c");
}

Pasting Cell Values

The paste operation is very similar to the copy 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. Below is the sample that demonstrates how you can perform these actions from scripts.

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, GridObj.Keys("^A")).

Example

View description

JavaScript

function Main ()
{
  var p, Grid, Level, ColumnName, ViewName;
  
  // Obtain the grid object
  p = Sys.Process("MySampleApp");
  Grid = p.VCLObject("FormName").VCLObject("GridName");

  // Obtain a child view
  Level = Grid.wChildLevel(2);

  // Place a value to clipboard
  Sys.Clipboard = "New Value";
  Log.Message ("The clipboard now contains : " + Sys.Clipboard);

  // Paste cell value
  ColumnName = "MyColumn";
  ViewName = "MyView";
  PasteValueToCell (Grid, Level, 4, ColumnName, ViewName);
}

// Paste the cell value
function PasteValueToCell (Grid, Level, RowIndex, ColumnId, ViewId)
{
  // Select the cell
  if (strictEqual(Level, null))
    Grid.ClickCell (RowIndex, ColumnId, ViewId);
  else
    Level.ClickCell (RowIndex, ColumnId, ViewId);

  // Simulate the F2 keystroke to activate the cell's in-place editor
  Grid.Keys("[F2]");
  // Simulate the Ctrl-V keystroke
  Grid.Keys("^v");
  // To paste value with the context menu item,
  // you can use the following code:
  // Grid.Keys("[Apps]p");
  // Simulate the Enter key press to apply the changes
  // Grid.Keys("[Enter]");
}

JScript

function Main ()
{
  var p, Grid, Level, ColumnName, ViewName;
  
  // Obtain the grid object
  p = Sys.Process("MySampleApp");
  Grid = p.VCLObject("FormName").VCLObject("GridName");

  // Obtain a child view
  Level = Grid.wChildLevel(2);

  // Place a value to clipboard
  Sys.Clipboard = "New Value";
  Log.Message ("The clipboard now contains : " + Sys.Clipboard);

  // Paste cell value
  ColumnName = "MyColumn";
  ViewName = "MyView";
  PasteValueToCell (Grid, Level, 4, ColumnName, ViewName);
}

// Paste the cell value
function PasteValueToCell (Grid, Level, RowIndex, ColumnId, ViewId)
{
  // Select the cell
  if (Level == null)
    Grid.ClickCell (RowIndex, ColumnId, ViewId);
  else
    Level.ClickCell (RowIndex, ColumnId, ViewId);

  // Simulate the F2 keystroke to activate the cell's in-place editor
  Grid.Keys("[F2]");
  // Simulate the Ctrl-V keystroke
  Grid.Keys("^v");
  // To paste value with the context menu item,
  // you can use the following code:
  // Grid.Keys("[Apps]p");
  // Simulate the Enter key press to apply the changes
  // Grid.Keys("[Enter]");
}

Python

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

  # Obtain a child view
  Level = Grid.wChildLevel(2)

  # Place a value to clipboard
  Sys.Clipboard = "New Value"
  Log.Message ("The clipboard now contains : " + Sys.Clipboard)

  # Paste cell value
  ColumnName = "MyColumn"
  ViewName = "MyView"
  PasteValueToCell (Grid, Level, 4, ColumnName, ViewName)

# Paste the cell value
def PasteValueToCell (Grid, Level, RowIndex, ColumnId, ViewId):
  # Select the cell
  if (Level == None): 
    Grid.ClickCell (RowIndex, ColumnId, ViewId)
  else:
    Level.ClickCell (RowIndex, ColumnId, ViewId)

  # Simulate the F2 keystroke to activate the cell's in-place editor
  Grid.Keys("[F2]")
  # Simulate the Ctrl-V keystroke
  Grid.Keys("^v")
  # To paste value with the context menu item, 
  # you can use the following code:
  # Grid.Keys("[Apps]p")
  # Simulate the Enter key press to apply the changes
  # Grid.Keys("[Enter]")

VBScript

Sub Main
  Dim p, Grid, Level, ColumnName, ViewName
  
  ' Obtain the grid object
  Set p = Sys.Process("MySampleApp")
  Set Grid = p.VCLObject("FormName").VCLObject("GridName")

  ' Obtain a child view
  Set Level = Grid.wChildLevel(2)

  ' Place a value to clipboard
  Sys.Clipboard = "New Value"
  Log.Message ("The clipboard now contains : " & Sys.Clipboard)

  ' Paste cell value
  ColumnName = "MyColumn"
  ViewName = "MyView"
  Call PasteValueToCell (Grid, Level, 4, ColumnName, ViewName)
End Sub

' Paste the cell value
Sub PasteValueToCell (Grid, Level, RowIndex, ColumnId, ViewId)
  ' Select the cell
  If Level Is Nothing Then
    Call Grid.ClickCell (RowIndex, ColumnId, ViewId)
  Else
    Call Level.ClickCell (RowIndex, ColumnId, ViewId)
  End If

  ' Simulate the F2 keystroke to activate the cell's in-place editor
  Grid.Keys("[F2]")
  ' Simulate the Ctrl-V keystroke
  Grid.Keys("^v")
  ' To paste value with the context menu item,
  ' you can use the following code:
  ' Grid.Keys("[Apps]p")
  ' Simulate the Enter key press to apply the changes
  Grid.Keys("[Enter]")
End Sub

DelphiScript

// Paste the cell value
procedure PasteValueToCell (Grid, Level, RowIndex, ColumnId, ViewId);
begin
  // Select the cell
  if Level = nil then 
    Grid.ClickCell (RowIndex, ColumnId, ViewId)
  else
    Level.ClickCell (RowIndex, ColumnId, ViewId);

  // Simulate the F2 keystroke to activate the cell's in-place editor
  Grid.Keys('[F2]');
  // Simulate the Ctrl-V keystroke
  Grid.Keys('^v');
  // To paste value with the context menu item,
  // you can use the following code:
  // Grid.Keys('[Apps]p');
  // Simulate the Enter key press to apply the changes
  Grid.Keys('[Enter]');
end;

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

  // Obtain a child view
  Level := Grid.wChildLevel[2];

  // Place a value to clipboard
  Sys.Clipboard := 'New Value';
  Log.Message ('The clipboard now contains : ' + Sys.Clipboard);

  // Paste cell value
  ColumnName := 'MyColumn';
  ViewName := 'MyView';
  PasteValueToCell (Grid, Level, 4, ColumnName, ViewName);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, Level, ColumnName, ViewName;

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

  // Obtain a child view
  Level = Grid["wChildLevel"](2);

  // Place a value to clipboard
  Sys["Clipboard"] = "New Value";
  Log["Message"]("The clipboard now contains : " + Sys["Clipboard"]);

  // Paste cell value
  ColumnName = "MyColumn";
  ViewName = "MyView";
  PasteValueToCell (Grid, Level, 4, ColumnName, ViewName);
}

// Paste the cell value
function PasteValueToCell (Grid, Level, RowIndex, ColumnId, ViewId)
{
  // Select the cell
  if (Level == null)
    Grid["ClickCell"](RowIndex, ColumnId, ViewId);
  else
    Level["ClickCell"](RowIndex, ColumnId, ViewId);

  // Simulate the F2 keystroke to activate the cell's in-place editor
  Grid["Keys"]("[F2]");
  // Simulate the Ctrl-V keystroke
  Grid["Keys"]("^v");
  // To paste value with the context menu item,
  // you can use the following code:
  // Grid["Keys"]("[Apps]p");
  // Simulate the Enter key press to apply the changes
  // Grid["Keys"]("[Enter]");
}

See Also

Working With Developer Express QuantumGrid
Obtaining and Setting Cell Values in Developer Express QuantumGrid
Selecting Cells in Developer Express QuantumGrid
Activating In-place Editors in Developer Express QuantumGrid

Highlight search results