Copying and Pasting Cell Values in Syncfusion GridControl

Applies to TestComplete 15.40, last modified on March 25, 2022

When testing an application with Syncfusion GridControl, you may need to copy or paste grid data to and from the clipboard. For example, you can use this to specify the same values in multiple cells or organize a data exchange between the tested application and another one. 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 the two approaches that you can use to copy and paste GridControl data from scripts; simulating the corresponding user actions over the grid control and using the grid’s internal properties and methods. In your scripts, you can use the approach that better suits your needs. Depending on the approach you choose, you may need to locate the desired cell in the grid and select it in order to be able to copy or paste the cell value. For more information on how you can obtain and modify grid data, see Obtaining and Setting Cell Values in Syncfusion GridControl.

To perform these actions, TestComplete should have access to internal objects, properties and methods of the GridControl object. For this purpose, the .NET Application Support plugin must be installed and enabled.

When testing Syncfusion GridControl controls, use specific methods and properties of the corresponding SyncfusionEssGrid 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 GridControl 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 User Actions Over the GridControl

You can copy the cell value to the clipboard or paste it from the clipboard using the Ctrl+C and Ctrl+V keyboard shortcuts. You can simulate these shortcuts using the Keys action applied to the grid control.

Before performing the copy or paste operation, you need to locate the desired 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 successive Home and Shift+End key presses (Home moves the caret to the beginning of the cell text and Shift+End selects the cell text from the caret position to the end). After performing the pasting operation, you should apply the changes by simulating the Enter keypress.

Note: The grid control may be configured so that it selects the cell contents right after the cell becomes selected. For example, this happens if the grid’s ActivateCurrentCellBehavior property is set to “SelectAll”. In this case, you do not need to perform any actions to select the cell text to be copied or replaced.

Below in an example that illustrates how you can copy the GridControl cells' values to and paste them from the clipboard.

Example

View description

JavaScript, JScript

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

  // Obtain the application process and the grid object
  p = Sys.Process("UndoRedoSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("panel1").WinFormsObject("gridControl1");

  // Copy the cell value and paste it to other cells
  CopyCellValue (Grid, 1, 1);
  Log.Message ("Copied value: " + Sys.Clipboard);
  for (i=2; i<=9; i++)
    PasteCellValue (Grid, i, 1);

  // Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard = "5000";
  PasteCellValue (Grid, 10, 1);
}

function CopyCellValue (Grid, RowIndex, ColIndex)
{
  // Select the cell and activate its in-place editor
  ClickCell (Grid, RowIndex, ColIndex);
  ActivateCellEditor (Grid);
  // Select the cell contents and copy it to the clipboard
  Grid.Keys ("[Home]![End]^c");
  // Close the cell's editor
  CancelEdit (Grid);
}

function PasteCellValue (Grid, RowIndex, ColIndex)
{
  // Select the cell and activate its in-place editor
  ClickCell (Grid, RowIndex, ColIndex);
  ActivateCellEditor (Grid);
  // Replace the cell value with the clipboard contents
  Grid.Keys ("[Home]![End]^v");
  // Save the changes
  CloseCellEditor (Grid);
}

function ClickCell (Grid, RowIndex, ColIndex)
{
  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get the cell coordinates
  var rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
}

function ActivateCellEditor (Grid)
{
  if (!Grid.CurrentCell.IsEditing)
    Grid.Keys ("[F2]")
}

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

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

Python

def Main ():

  # Obtain the application process and the grid object
  p = Sys.Process("UndoRedoSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("panel1").WinFormsObject("gridControl1");

  # Copy the cell value and paste it to other cells
  CopyCellValue (Grid, 1, 1);
  Log.Message ("Copied value: " + Sys.Clipboard); 
  for i in range(2, 9):
    PasteCellValue (Grid, i, 1);

  # Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard = "5000";
  PasteCellValue (Grid, 10, 1);

def CopyCellValue (Grid, RowIndex, ColIndex):
  # Select the cell and activate its in-place editor
  ClickCell (Grid, RowIndex, ColIndex);
  ActivateCellEditor (Grid);
  # Select the cell contents and copy it to the clipboard
  Grid.Keys ("[Home]![End]^c");
  # Close the cell's editor
  CancelEdit (Grid);

def PasteCellValue (Grid, RowIndex, ColIndex):
  # Select the cell and activate its in-place editor
  ClickCell (Grid, RowIndex, ColIndex);
  ActivateCellEditor (Grid);
  # Replace the cell value with the clipboard contents 
  Grid.Keys ("[Home]![End]^v");
  # Save the changes
  CloseCellEditor (Grid);

def ClickCell (Grid, RowIndex, ColIndex):
  # Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  # Get the cell coordinates
  rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);

def ActivateCellEditor (Grid):
  if not Grid.CurrentCell.IsEditing:
    Grid.Keys ("[F2]")

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

def CancelEdit (Grid):
  Grid.Keys ("[Esc]");

VBScript

Sub Main
  Dim p, Grid, i

  ' Obtain the application process and the grid object
  Set p = Sys.Process("UndoRedoSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("panel1").WinFormsObject("gridControl1")

  ' Copy the cell value and paste it to other cells
  Call CopyCellValue (Grid, 1, 1)
  Call Log.Message ("Copied value: " & Sys.Clipboard)
  For i = 2 To 9
    Call PasteCellValue (Grid, i, 1)
  Next

  ' Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard = "5000"
  Call PasteCellValue (Grid, 10, 1)
End Sub

Sub CopyCellValue (Grid, RowIndex, ColIndex)
  ' Select the cell and activate its in-place editor
  Call ClickCell (Grid, RowIndex, ColIndex)
  Call ActivateCellEditor (Grid)
  ' Select the cell contents and copy it to the clipboard
  Call Grid.Keys ("[Home]![End]^c")
  ' Close the cell's editor
  Call CancelEdit (Grid)
End Sub

Sub PasteCellValue (Grid, RowIndex, ColIndex)
  ' Select the cell and activate its in-place editor
  Call ClickCell (Grid, RowIndex, ColIndex)
  Call ActivateCellEditor (Grid)
  ' Replace the cell value with the clipboard contents
  Call Grid.Keys ("[Home]![End]^v")
  ' Save the changes
  Call CloseCellEditor (Grid)
End Sub

Sub ClickCell (Grid, RowIndex, ColIndex)
  Dim rect

  ' Make cell visible
  Call Grid.ScrollCellInView_3 (RowIndex, ColIndex)

  ' Get the cell coordinates
  Set rect = Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex))

  Call Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2)
End Sub

Sub ActivateCellEditor (Grid)
  If Not Grid.CurrentCell.IsEditing Then
    Call Grid.Keys ("[F2]")
  End If
End Sub

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

Sub CancelEdit (Grid)
  Call Grid.Keys ("[Esc]")
End Sub

DelphiScript

procedure CopyCellValue (Grid, RowIndex, ColIndex); forward;
procedure PasteCellValue (Grid, RowIndex, ColIndex); forward;
procedure ClickCell (Grid, RowIndex, ColIndex); forward;
procedure ActivateCellEditor (Grid); forward;
procedure CloseCellEditor (Grid); forward;
procedure CancelEdit (Grid); forward;

procedure Main;
var p, Grid, i : OleVariant;
begin
  // Obtain the application process and the grid object
  p := Sys.Process('UndoRedoSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('panel1').WinFormsObject('gridControl1');

  // Copy the cell value and paste it to other cells
  CopyCellValue (Grid, 1, 1);
  Log.Message ('Copied value: ' + Sys.Clipboard);
  for i:=2 to 9 do
    PasteCellValue (Grid, i, 1);

  // Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard := '5000';
  PasteCellValue (Grid, 10, 1);
end;

procedure CopyCellValue (Grid, RowIndex, ColIndex);
begin
  // Select the cell and activate its in-place editor
  ClickCell (Grid, RowIndex, ColIndex);
  ActivateCellEditor (Grid);
  // Select the cell contents and copy it to the clipboard
  Grid.Keys ('[Home]![End]^c');
  // Close the cell's editor
  CancelEdit (Grid);
end;

procedure PasteCellValue (Grid, RowIndex, ColIndex);
begin
  // Select the cell and activate its in-place editor
  ClickCell (Grid, RowIndex, ColIndex);
  ActivateCellEditor (Grid);
  // Replace the cell value with the clipboard contents
  Grid.Keys ('[Home]![End]^v');
  // Save the changes
  CloseCellEditor (Grid);
end;

procedure ClickCell (Grid, RowIndex, ColIndex);
var rect : OleVariant;
begin
  // Make cell visible
  Grid.ScrollCellInView_3 (RowIndex, ColIndex);

  // Get the cell coordinates
  rect := Grid.RangeInfoToRectangle (Grid.GridCellsRange.Cell(RowIndex, ColIndex));

  Grid.Click (rect.X + rect.Width/2, rect.Y + rect.Height/2);
end;

procedure ActivateCellEditor (Grid);
begin
  if not Grid.CurrentCell.IsEditing then
    Grid.Keys ('[F2]')
end;

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

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

C++Script, C#Script

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

  // Obtain the application process and the grid object
  p = Sys.Process("UndoRedoSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("panel1").WinFormsObject("gridControl1");

  // Copy the cell value and paste it to other cells
  CopyCellValue (Grid, 1, 1);
  Log["Message"]("Copied value: " + Sys["Clipboard"]);
  for (i=2; i<=9; i++)
    PasteCellValue (Grid, i, 1);

  // Set the clipboard contents directly and paste it into the cell
  Sys["Clipboard"] = "5000";
  PasteCellValue (Grid, 10, 1);
}

function CopyCellValue (Grid, RowIndex, ColIndex)
{
  // Select the cell and activate its in-place editor
  ClickCell (Grid, RowIndex, ColIndex);
  ActivateCellEditor (Grid);
  // Select the cell contents and copy it to the clipboard
  Grid["Keys"]("[Home]![End]^c");
  // Close the cell's editor
  CancelEdit (Grid);
}

function PasteCellValue (Grid, RowIndex, ColIndex)
{
  // Select the cell and activate its in-place editor
  ClickCell (Grid, RowIndex, ColIndex);
  ActivateCellEditor (Grid);
  // Replace the cell value with the clipboard contents
  Grid["Keys"]("[Home]![End]^v");
  // Save the changes
  CloseCellEditor (Grid);
}

function ClickCell (Grid, RowIndex, ColIndex)
{
  // Make cell visible
  Grid["ScrollCellInView_3"](RowIndex, ColIndex);

  // Get the cell coordinates
  var rect = Grid["RangeInfoToRectangle"](Grid["GridCellsRange"]["Cell"](RowIndex, ColIndex));

  Grid["Click"](rect["X"] + rect["Width"]/2, rect["Y"] + rect["Height"]/2);
}

function ActivateCellEditor (Grid)
{
  if (! Grid["CurrentCell"]["IsEditing"])
    Grid["Keys"]("[F2]")
}

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

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

GridControl may also have a context menu allowing the users to copy or paste grid cell values, so you may want to perform clipboard operations using this menu. To call the context menu, you can use the ClickR action applied to the grid control, or send the Application key press to the grid control using the Keys action. After the context menu is displayed on the screen, you can obtain it using the PopupMenu property added to the GridControl by TestComplete. To select items in the context menu, use the Click or Select method of the resulting Menu object.

Using the GridControl Internal Methods

It is possible to copy and paste GridControl cell values by using the grid control's internal methods. The grid’s Model.CutPaste property provides access to the special GridModelCutPaste object that lets you perform various clipboard operations with the grid data. For example, to copy the selected cell’s value to the clipboard and paste it from the clipboard, you can use the following statements:

GridObj.Model.CutPaste.Copy()
GridObj.Model.CutPaste.Paste()

Note that these methods only work with the currently selected cell. To be able to copy or paste the cell value you need to locate the cell in the grid and select it.

The example below demonstrates how you can copy and paste cell data via the GridControl.Model.CutPaste object in scripts:

Example

View description

JavaScript, JScript

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

  // Obtain the application process and the grid object
  p = Sys.Process("UndoRedoSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("panel1").WinFormsObject("gridControl1");

  // Copy the cell value and paste it to other cells
  CopyCellValue (Grid, 1, 1);
  Log.Message ("Copied value: " + Sys.Clipboard);
  for (i=2; i<=9; i++)
    PasteCellValue (Grid, i, 1);

  // Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard = "5000";
  PasteCellValue (Grid, 10, 1);
}

function CopyCellValue (Grid, RowIndex, ColIndex)
{
  SelectCell (Grid, RowIndex, ColIndex);
  Grid.Model.CutPaste.Copy();
}

function PasteCellValue (Grid, RowIndex, ColIndex)
{
  SelectCell (Grid, RowIndex, ColIndex);
  Grid.Model.CutPaste.Paste();
}

function SelectCell (Grid, RowIndex, ColIndex)
{
  // Try to navigate to the specified cell
  if (Grid.CurrentCell.MoveTo_3 (RowIndex, ColIndex))
    // Make the selected cell visible
    Grid.CurrentCell.ScrollInView()
  else
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") could not be selected.");
}

Python

def Main ():

  # Obtain the application process and the grid object
  p = Sys.Process("UndoRedoSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("panel1").WinFormsObject("gridControl1");

  # Copy the cell value and paste it to other cells
  CopyCellValue (Grid, 1, 1);
  Log.Message ("Copied value: " + Sys.Clipboard); 
  for i in range(2, 9):
    PasteCellValue (Grid, i, 1);

  # Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard = "5000";
  PasteCellValue (Grid, 10, 1);

def CopyCellValue (Grid, RowIndex, ColIndex):
  SelectCell (Grid, RowIndex, ColIndex);
  Grid.Model.CutPaste.Copy();

def PasteCellValue (Grid, RowIndex, ColIndex):
  SelectCell (Grid, RowIndex, ColIndex);
  Grid.Model.CutPaste.Paste();

def SelectCell (Grid, RowIndex, ColIndex):
  # Try to navigate to the specified cell
  if (Grid.CurrentCell.MoveTo_3 (RowIndex, ColIndex)):
    # Make the selected cell visible
    Grid.CurrentCell.ScrollInView()
  else:
    Log.Error ("Cell (" + RowIndex + ", " + ColIndex + ") could not be selected.");

VBScript

Sub Main
  Dim p, Grid, i

  ' Obtain the application process and the grid object
  Set p = Sys.Process("UndoRedoSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("panel1").WinFormsObject("gridControl1")

  ' Copy the cell value and paste it to other cells
  Call CopyCellValue (Grid, 1, 1)
  Call Log.Message ("Copied value: " & Sys.Clipboard)
  For i = 2 To 9
    Call PasteCellValue (Grid, i, 1)
  Next

  ' Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard = "5000"
  Call PasteCellValue (Grid, 10, 1)
End Sub

Sub CopyCellValue (Grid, RowIndex, ColIndex)
  Call SelectCell (Grid, RowIndex, ColIndex)
  Grid.Model.CutPaste.Copy
End Sub

Sub PasteCellValue (Grid, RowIndex, ColIndex)
  Call SelectCell (Grid, RowIndex, ColIndex)
  Grid.Model.CutPaste.Paste
End Sub

Sub SelectCell (Grid, RowIndex, ColIndex)
  ' Try to navigate to the specified cell
  If Grid.CurrentCell.MoveTo_3 (RowIndex, ColIndex) Then
    ' Make the selected cell visible
    Grid.CurrentCell.ScrollInView
  Else
    Call Log.Error ("Cell (" & RowIndex & ", " & ColIndex & ") could not be selected.")
  End If
End Sub

DelphiScript

procedure CopyCellValue (Grid, RowIndex, ColIndex); forward;
procedure PasteCellValue (Grid, RowIndex, ColIndex); forward;
procedure SelectCell (Grid, RowIndex, ColIndex); forward;

procedure Main;
var p, Grid, i : OleVariant;
begin
  // Obtain the application process and the grid object
  p := Sys.Process('UndoRedoSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('panel1').WinFormsObject('gridControl1');

  // Copy the cell value and paste it to other cells
  CopyCellValue (Grid, 1, 1);
  Log.Message ('Copied value: ' + Sys.Clipboard);
  for i:=2 to 9 do
    PasteCellValue (Grid, i, 1);

  // Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard := '5000';
  PasteCellValue (Grid, 10, 1);
end;

procedure CopyCellValue (Grid, RowIndex, ColIndex);
begin
  SelectCell (Grid, RowIndex, ColIndex);
  Grid.Model.CutPaste.Copy;
end;

procedure PasteCellValue (Grid, RowIndex, ColIndex);
begin
  SelectCell (Grid, RowIndex, ColIndex);
  Grid.Model.CutPaste.Paste;
end;

procedure SelectCell (Grid, RowIndex, ColIndex);
begin
  // Try to navigate to the specified cell
  if Grid.CurrentCell.MoveTo_3 (RowIndex, ColIndex) then
    // Make the selected cell visible
    Grid.CurrentCell.ScrollInView
  else
    Log.Error ('Cell (' + aqConvert.VarToStr(RowIndex) + ', ' + aqConvert.VarToStr(ColIndex) + ') could not be selected.');
end;

C++Script, C#Script

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

  // Obtain the application process and the grid object
  p = Sys["Process"]("UndoRedoSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("panel1")["WinFormsObject"]("gridControl1");

  // Copy the cell value and paste it to other cells
  CopyCellValue (Grid, 1, 1);
  Log["Message"]("Copied value: " + Sys["Clipboard"]);
  for (i=2; i<=9; i++)
    PasteCellValue (Grid, i, 1);

  // Set the clipboard contents directly and paste it into the cell
  Sys["Clipboard"] = "5000";
  PasteCellValue (Grid, 10, 1);
}

function CopyCellValue (Grid, RowIndex, ColIndex)
{
  SelectCell (Grid, RowIndex, ColIndex);
  Grid["Model"]["CutPaste"]["Copy"]();
}

function PasteCellValue (Grid, RowIndex, ColIndex)
{
  SelectCell (Grid, RowIndex, ColIndex);
  Grid["Model"]["CutPaste"]["Paste"]();
}

function SelectCell (Grid, RowIndex, ColIndex)
{
  // Try to navigate to the specified cell
  if (Grid["CurrentCell"]["MoveTo_3"](RowIndex, ColIndex))
    // Make the selected cell visible
    Grid["CurrentCell"]["ScrollInView"]()
  else
    Log["Error"]("Cell (" + RowIndex + ", " + ColIndex + ") could not be selected.");
}

See Also

Working With Syncfusion GridControl
Selecting Cells in Syncfusion GridControl
Activating and Closing In-place Editors in Syncfusion GridControl
Obtaining and Setting Cell Values in Syncfusion GridControl

Highlight search results