Copying and Pasting Cell Values in Developer Express XtraGrid

Applies to TestComplete 15.42, last modified on September 08, 2022

When testing an application with the XtraGrid control, you may need to copy or paste grid data to and from the clipboard. For example, you can use this to organize a data exchange between the tested application and another one. In addition, the XtraGrid control lets you copy the contents of the selected records to the clipboard, so you may use this to export the selected data. With TestComplete, you can directly access the clipboard contents using the Sys.Clipboard property. This way, you can get the text copied to the clipboard as well as set the clipboard contents in order to paste it to the application under test.

This topic describes how you can copy and paste XtraGrid data from scripts:

To perform these actions, TestComplete should have access to internal objects, properties and methods of the XtraGrid control. For this purpose, the .NET Application Support and Developer Express Control Support plugins must be installed and enabled. The latter lets you work with the XtraGrid control using methods and properties of the DevExpressXtraGrid object. Without this plugin, you will not be able to work with XtraGrid controls using their internal methods and properties.

When testing Developer Express XtraGrid controls, use specific methods and properties of the corresponding DevExpressXtraGrid 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 an XtraGrid 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.

Note: The script samples in this topic work with the GridTutorials sample application shipped with the Developer Express XtraGrid Suite.

Copying and Pasting Individual Cell Values

When editing grid data, you may need to specify the same values in multiple cells. You can do this by copying a value of a particular cell and pasting it to the desired cells. At that, you can also directly access the clipboard contents via the Sys.Clipboard property and use it as you wish.

Before copying or pasting the cell value, you need to locate the needed cell in the grid, select it and activate its in-place editor. After the cell’s edit mode is activated, you should specify the cell contents to be copied or replaced. For example, to select the entire cell value, you can simulate the Ctrl+A shortcut.

To copy the selection to the clipboard or paste it from the clipboard, you can either simulate the Ctrl+C and Ctrl+V keyboard shortcuts, or select the Copy or Paste command from the cell’s context menu. To simulate the shortcuts, use the Keys action applied to the grid control. To work with the popup menu you can also use the corresponding shortcuts - the Application keypress will invoke the menu, the c keypress will select the Copy command and pressing p will select the Paste command (c and p are the hot keys for the context menu’s Copy and Paste items).

Note that after you have pasted the value into the cell, you should simulate the Enter keypress to apply the changes.

Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript

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

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Copy the cell value to the clipboard
  CopyCellValue (Grid, null, 0, "Unit Price");
  Log.Message ("Copied value: " + Sys.Clipboard);

  // Paste the copied value to other cells
  for (let i=1; i<=5; i++)
    PasteCellValue (Grid, null, i, "Unit Price");
}

function CopyCellValue (Grid, ActiveViewObj, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (strictEqual(ActiveViewObj, null))
    ActiveViewObj = Grid

  // Select the specified cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId);
  // Acivate the edit mode
  ActivateCellEditor (Grid);

  // Copy the cell contents using the Ctrl+C shortcut
  Grid.Keys ("^a^c");
  // You can also copy the cell value via the context menu --
  // Grid.Keys ("^a[Apps]c");

  CancelEditing (Grid);
}

function PasteCellValue (Grid, ActiveViewObj, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (strictEqual(ActiveViewObj, null))
    ActiveViewObj = Grid

  // Select the specified cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId);
  // Acivate the edit mode
  ActivateCellEditor (Grid);

  // Paste the cell contents using the Ctrl+V shortcut
  Grid.Keys ("^a^v");
  // You can also paste the cell value via the context menu --
  // Grid.Keys ("^a[Apps]p");

  // Save the changes
  CloseCellEditor (Grid);
}

function ActivateCellEditor (Grid)
{
  Grid.Keys ("[F2]");
}

function CloseCellEditor (Grid)
{
  Grid.Keys ("[Enter]");
}

function CancelEditing (Grid)
{
  Grid.Keys ("[Esc]");
}

JScript

function Main ()
{
  var p, frmMain, Grid, i;

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Copy the cell value to the clipboard
  CopyCellValue (Grid, null, 0, "Unit Price");
  Log.Message ("Copied value: " + Sys.Clipboard);

  // Paste the copied value to other cells
  for (i=1; i<=5; i++)
    PasteCellValue (Grid, null, i, "Unit Price");
}

function CopyCellValue (Grid, ActiveViewObj, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (ActiveViewObj == null)
    ActiveViewObj = Grid

  // Select the specified cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId);
  // Acivate the edit mode
  ActivateCellEditor (Grid);

  // Copy the cell contents using the Ctrl+C shortcut
  Grid.Keys ("^a^c");
  // You can also copy the cell value via the context menu --
  // Grid.Keys ("^a[Apps]c");

  CancelEditing (Grid);
}

function PasteCellValue (Grid, ActiveViewObj, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (ActiveViewObj == null)
    ActiveViewObj = Grid

  // Select the specified cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId);
  // Acivate the edit mode
  ActivateCellEditor (Grid);

  // Paste the cell contents using the Ctrl+V shortcut
  Grid.Keys ("^a^v");
  // You can also paste the cell value via the context menu --
  // Grid.Keys ("^a[Apps]p");

  // Save the changes
  CloseCellEditor (Grid);
}

function ActivateCellEditor (Grid)
{
  Grid.Keys ("[F2]");
}

function CloseCellEditor (Grid)
{
  Grid.Keys ("[Enter]");
}

function CancelEditing (Grid)
{
  Grid.Keys ("[Esc]");
}

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridTutorials")
  frmMain = p.WinFormsObject("frmMain")
  # Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Copy the cell value to the clipboard
  CopyCellValue (Grid, None, 0, "Unit Price")
  Log.Message ("Copied value: " + Sys.Clipboard)

  # Paste the copied value to other cells
  for i in range(1, 5):
    PasteCellValue (Grid, None, i, "Unit Price")

def CopyCellValue (Grid, ActiveViewObj, RowIndex, ColumnId):
  # Get the grid view, if it is not specified
  if (ActiveViewObj == None): 
    ActiveViewObj = Grid

  # Select the specified cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId)
  # Acivate the edit mode
  ActivateCellEditor (Grid)

  # Copy the cell contents using the Ctrl+C shortcut
  Grid.Keys ("^a^c")
  # You can also copy the cell value via the context menu --
  # Grid.Keys ("^a[Apps]c")

  CancelEditing (Grid)

def PasteCellValue (Grid, ActiveViewObj, RowIndex, ColumnId):
  # Get the grid view, if it is not specified
  if (ActiveViewObj == None): 
    ActiveViewObj = Grid

  # Select the specified cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId)
  # Acivate the edit mode
  ActivateCellEditor (Grid)

  # Paste the cell contents using the Ctrl+V shortcut
  Grid.Keys ("^a^v")
  # You can also paste the cell value via the context menu --
  # Grid.Keys ("^a[Apps]p")

  # Save the changes
  CloseCellEditor (Grid)

def ActivateCellEditor (Grid):
  Grid.Keys ("[F2]")

def CloseCellEditor (Grid):
  Grid.Keys ("[Enter]")

def CancelEditing (Grid):
  Grid.Keys ("[Esc]")

VBScript

Sub Main
  Dim p, frmMain, Grid, i

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridTutorials")
  Set frmMain = p.WinFormsObject("frmMain")
  ' Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' Copy the cell value to the clipboard
  Call CopyCellValue (Grid, Nothing, 0, "Unit Price")
  Log.Message "Copied value: " & Sys.Clipboard

  ' Paste the copied value to other cells
  For i = 1 To 5
    Call PasteCellValue (Grid, Nothing, i, "Unit Price")
  Next
End Sub

Sub CopyCellValue (Grid, ActiveViewObj, RowIndex, ColumnId)
  ' Get the grid view, if it is not specified
  If ActiveViewObj Is Nothing Then
    Set ActiveViewObj = Grid
  End If
  ' Select the specified cell
  Call ActiveViewObj.ClickCell(RowIndex, ColumnId)
  ' Acivate the edit mode
  Call ActivateCellEditor(Grid)

  ' Copy the cell contents using the Ctrl+C shortcut
  Call Grid.Keys ("^a^c")
  ' You can also copy the cell value via the context menu --
  'Call Grid.Keys("^a[Apps]c")

  Call CancelEditing(Grid)
End Sub

Sub PasteCellValue (Grid, ActiveViewObj, RowIndex, ColumnId)
  ' Get the grid view, if it is not specified
  If ActiveViewObj Is Nothing Then
    Set ActiveViewObj = Grid
  End If
  ' Select the specified cell
  Call ActiveViewObj.ClickCell (RowIndex, ColumnId)
  ' Acivate the edit mode
  Call ActivateCellEditor(Grid)

  ' Paste the cell contents using the Ctrl+V shortcut
  Call Grid.Keys("^a^v")
  ' You can also paste the cell value via the context menu --
  'Call Grid.Keys("^a[Apps]p")

  ' Save the changes
  Call CloseCellEditor(Grid)
End Sub

Sub ActivateCellEditor (Grid)
  Grid.Keys "[F2]"
End Sub

Sub CloseCellEditor (Grid)
  Grid.Keys "[Enter]"
End Sub

Sub CancelEditing (Grid)
  Grid.Keys "[Esc]"
End Sub

DelphiScript

procedure CopyCellValue (Grid, ActiveViewObj, RowIndex, ColumnId: OleVariant); forward;
procedure PasteCellValue (Grid, ActiveViewObj, RowIndex, ColumnId: OleVariant); forward;
procedure ActivateCellEditor (Grid); forward;
procedure CloseCellEditor (Grid); forward;
procedure CancelEditing (Grid); forward;

procedure Main;
var p, frmMain, Grid, i : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridTutorials');
  frmMain := p.WinFormsObject('frmMain');
  // Select the 'Multi Select' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Multi Select';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Copy the cell value to the clipboard
  CopyCellValue (Grid, nil, 0, 'Unit Price');
  Log.Message ('Copied value: ' + Sys.Clipboard);

  // Paste the copied value to other cells
  for i := 1 to 5 do
    PasteCellValue (Grid, nil, i, 'Unit Price');
end;

procedure CopyCellValue (Grid, ActiveViewObj, RowIndex, ColumnId: OleVariant);
begin
  // Get the grid view, if it is not specified
  if (ActiveViewObj=nil) then 
    ActiveViewObj := Grid;
  // Select the specified cell
  ActiveViewObj.ClickCell (RowIndex, ColumnId);
  // Acivate the edit mode
  ActivateCellEditor (Grid);

  // Copy the cell contents using the Ctrl+C shortcut
  Grid.Keys ('^a^c');
  // You can also copy the cell value via the context menu --
  // Grid.Keys ('^a[Apps]c');

  CancelEditing (Grid);
end;

procedure PasteCellValue (Grid, ActiveViewObj, RowIndex, ColumnId: OleVariant);
begin
  // Get the grid view, if it is not specified
  if (ActiveViewObj=nil) then 
    ActiveViewObj := Grid;
  // Select the specified cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Acivate the edit mode
  ActivateCellEditor (Grid);

  // Paste the cell contents using the Ctrl+V shortcut
  Grid.Keys ('^a^v');
  // You can also paste the cell value via the context menu --
  // Grid.Keys ('^a[Apps]p');

  // Save the changes
  CloseCellEditor (Grid);
end;

procedure ActivateCellEditor (Grid);
begin
  Grid.Keys ('[F2]');
end;

procedure CloseCellEditor (Grid);
begin
  Grid.Keys ('[Enter]');
end;

procedure CancelEditing (Grid);
begin
  Grid.Keys ('[Esc]');
end;

C++Script, C#Script

function Main ()
{
  var p, frmMain, Grid, i;

  // Obtain the application process and its main form
  p = Sys["Process"]("GridTutorials");
  frmMain = p["WinFormsObject"]("frmMain");
  // Select the "Multi Select" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Multi Select";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Copy the cell value to the clipboard
  CopyCellValue (Grid, null, 0, "Unit Price");
  Log["Message"] ("Copied value: " + Sys["Clipboard"]);

  // Paste the copied value to other cells
  for (i=1; i<=5; i++)
    PasteCellValue (Grid, null, i, "Unit Price");
}

function CopyCellValue (Grid, ActiveViewObj, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (ActiveViewObj == null)
    ActiveViewObj = Grid

  // Select the specified cell
  ActiveViewObj["ClickCell"] (RowIndex, ColumnId);
  // Acivate the edit mode
  ActivateCellEditor (Grid);

  // Copy the cell contents using the Ctrl+C shortcut
  Grid["Keys"] ("^a^c");
  // You can also copy the cell value via the context menu --
  // Grid["Keys"] ("^a[Apps]c");

  CancelEditing (Grid);
}

function PasteCellValue (Grid, ActiveViewObj, RowIndex, ColumnId)
{
  // Get the grid view, if it is not specified
  if (ActiveViewObj == null)
    ActiveViewObj = Grid

  // Select the specified cell
  ActiveViewObj["ClickCell"] (RowIndex, ColumnId);
  // Acivate the edit mode
  ActivateCellEditor (Grid);

  // Paste the cell contents using the Ctrl+V shortcut
  Grid["Keys"] ("^a^v");
  // You can also paste the cell value via the context menu --
  // Grid["Keys"]("^a[Apps]p");

  // Save the changes
  CloseCellEditor (Grid);
}

function ActivateCellEditor (Grid)
{
  Grid["Keys"] ("[F2]");
}

function CloseCellEditor (Grid)
{
  Grid["Keys"] ("[Enter]");
}

function CancelEditing (Grid)
{
  Grid["Keys"] ("[Esc]");
}

Copying Selected Records

The XtraGrid control lets the users copy contents of the selected records (rows, cards or individual cells) to the clipboard as a tab-delimited text, so that it can be easily used by other applications, such as Notepad or Microsoft Excel. The records are copied when the user presses the Ctrl+C shortcut. You can send this keystroke to the grid control using the Keys action.

Example

The example works with the GridTutorials application.

How to get the application

View example description

JavaScript, JScript

function Main ()
{
  var p, frmMain, Grid, FileName;

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Select a range of rows
  SelectRange (Grid, 3, 21);

  // Copy selected records to the clipboard
  Grid.Keys ("^c");

  // Save the clipboard contents to a file
  FileName = "C:\\SelectedRecords.txt";
  aqFile.WriteToTextFile (FileName, Sys.Clipboard, aqFile.ctANSI, true);
  Log.File (FileName, "Selected records");
}

// Selects the specified range of rows
function SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex)
{

  // Select the first row of the range
  ActiveViewObj.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  ActiveViewObj.ClickRowIndicator (EndRowIndex, skShift);
}

Python

def Main ():

  # Obtain the application process and its main form
  p = Sys.Process("GridTutorials")
  frmMain = p.WinFormsObject("frmMain")
  # Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Select a range of rows
  SelectRange (Grid, 3, 21)

  # Copy selected records to the clipboard
  Grid.Keys ("^c")

  # Save the clipboard contents to a file
  FileName = "C:\\SelectedRecords.txt"
  aqFile.WriteToTextFile (FileName, Sys.Clipboard, aqFile.ctANSI, True)
  Log.File (FileName, "Selected records") 

# Selects the specified range of rows
def SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex):

  # Select the first row of the range
  ActiveViewObj.ClickRowIndicator (StartRowIndex)
  # Extend the selection
  ActiveViewObj.ClickRowIndicator (EndRowIndex, skShift)

VBScript

Sub Main
  Dim p, frmMain, Grid, FileName

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridTutorials")
  Set frmMain = p.WinFormsObject("frmMain")
  ' Select the "Multi Select" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Multi Select"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' Select a range of rows
  Call SelectRange (Grid, 3, 21)

  ' Copy selected records to the clipboard
  Call Grid.Keys ("^c")

  ' Save the clipboard contents to a file
  FileName = "C:\\SelectedRecords.txt"
  Call aqFile.WriteToTextFile (FileName, Sys.Clipboard, aqFile.ctANSI, True)
  Call Log.File (FileName, "Selected records")
End Sub

' Selects the specified range of rows
Sub SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex)
  ' Select the first row of the range
  Call ActiveViewObj.ClickRowIndicator (StartRowIndex)
  ' Extend the selection
  Call ActiveViewObj.ClickRowIndicator (EndRowIndex, skShift)
End Sub

DelphiScript

// Selects the specified range of rows
procedure SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex: OleVariant);
begin
  // Select the first row of the range
  ActiveViewObj.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  ActiveViewObj.ClickRowIndicator (EndRowIndex, skShift);
end;

procedure Main;
var p, frmMain, Grid, FileName : OleVariant;
begin
  // Obtain the application process and its main form
  p := Sys.Process('GridTutorials');
  frmMain := p.WinFormsObject('frmMain');
  // Select the 'Multi Select' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Multi Select';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Select a range of rows
  SelectRange (Grid, 3, 21);

  // Copy selected records to the clipboard
  Grid.Keys ('^c');

  // Save the clipboard contents to a file
  FileName := 'C:\SelectedRecords.txt';
  aqFile.WriteToTextFile (FileName, Sys.Clipboard, aqFile.ctANSI, true);
  Log.File (FileName, 'Selected records');
end;

C++Script, C#Script

function Main ()
{
  var p, frmMain, Grid, FileName;

  // Obtain the application process and its main form
  p = Sys["Process"]("GridTutorials");
  frmMain = p["WinFormsObject"]("frmMain");
  // Select the "Multi Select" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Multi Select";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Select a range of rows
  SelectRange (Grid, 3, 21);

  // Copy selected records to the clipboard
  Grid["Keys"]("^c");

  // Save the clipboard contents to a file
  FileName = "C:\\SelectedRecords.txt";
  aqFile["WriteToTextFile"](FileName, Sys["Clipboard"], aqFile["ctANSI"], true);
  Log["File"](FileName, "Selected records");
}

// Selects the specified range of rows
function SelectRange (ActiveViewObj, StartRowIndex, EndRowIndex)
{

  // Select the first row of the range
  ActiveViewObj["ClickRowIndicator"](StartRowIndex);
  // Extend the selection
  ActiveViewObj["ClickRowIndicator"](EndRowIndex, skShift);
}

See Also

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

Highlight search results