Copying and Pasting Cell Values in Syncfusion GridGroupingControl

Applies to TestComplete 15.0, last modified on October 22, 2021

When testing an application with Syncfusion GridGroupingControl, 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.

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). To be able to paste the clipboard contents to the cell, you need to clear the cell’s value, for example, by simulating the Del key after selecting the cell’s contents. 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 Model.Options.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 GridGroupingControl cells’ values to and paste them from the clipboard.

Example

View description

JavaScript

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

  // Obtain the grid object
  p = Sys.Process("EmployeeTerritoryOrder");
  p.WinFormsObject("Form1").Maximize();
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridGroupingControl1");
  ChildTable = Grid.wChildView(0, "Orders");

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

  // Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard = "4/20/2007 8:00 AM";
  PasteCellValue (Grid, ChildTable, 0, "ShippedDate");
}

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

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

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

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

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

JScript

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

  // Obtain the grid object
  p = Sys.Process("EmployeeTerritoryOrder");
  p.WinFormsObject("Form1").Maximize();
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridGroupingControl1");
  ChildTable = Grid.wChildView(0, "Orders");

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

  // Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard = "4/20/2007 8:00 AM";
  PasteCellValue (Grid, ChildTable, 0, "ShippedDate");
}

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

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

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

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

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

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process("EmployeeTerritoryOrder")
  p.WinFormsObject("Form1").Maximize()
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridGroupingControl1")
  ChildTable = Grid.wChildView[0, "Orders"]

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

  # Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard = "4/20/2007 8:00 AM"
  PasteCellValue (Grid, ChildTable, 0, "ShippedDate")

def CopyCellValue (Grid, View, RowIndex, ColumnId):
  # Select the cell and activate its in-place editor
  if (View == None):
    Grid.ClickCell (RowIndex, ColumnId)
  else:
    View.ClickCell (RowIndex, ColumnId)
  ActivateCellEditor (Grid)
  # Select the cell contents and copy it to the clipboard
  Grid.Keys ("[Home]![End]^c")
  # Close the cell editor
  CancelEdit (Grid)

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

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

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

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

VBScript

Sub Main
  Dim p, Grid, ChildTable, i

  ' Obtain the grid object
  Set p = Sys.Process("EmployeeTerritoryOrder")
  p.WinFormsObject("Form1").Maximize
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridGroupingControl1")
  Set ChildTable = Grid.wChildView(0, "Orders")

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

  ' Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard = "4/20/2007 8:00 AM"
  Call PasteCellValue (Grid, ChildTable, 0, "ShippedDate")
End Sub

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

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

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

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

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

DelphiScript

procedure CopyCellValue (Grid, View, RowIndex, ColumnId); forward;
procedure PasteCellValue (Grid, View, RowIndex, ColumnId); forward;
procedure ActivateCellEditor (Grid); forward;
procedure CloseCellEditor (Grid); forward;
procedure CancelEdit (Grid); forward;

procedure Main;
var p, Grid, ChildTable, i : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('EmployeeTerritoryOrder');
  p.WinFormsObject('Form1').Maximize;
  Grid := p.WinFormsObject('Form1').WinFormsObject('gridGroupingControl1');
  ChildTable := Grid.wChildView[0, 'Orders'];

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

  // Set the clipboard contents directly and paste it into the cell
  Sys.Clipboard := '4/20/2007 8:00 AM';
  PasteCellValue (Grid, ChildTable, 0, 'ShippedDate');
end;

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

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

procedure ActivateCellEditor (Grid);
begin
  if not Grid.TableControl.GetNestedCurrentCell.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, ChildTable, i;

  // Obtain the grid object
  p = Sys["Process"]("EmployeeTerritoryOrder");
  p["WinFormsObject"]("Form1")["Maximize"]();
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridGroupingControl1");
  ChildTable = Grid["wChildView"](0, "Orders");

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

  // Set the clipboard contents directly and paste it into the cell
  Sys["Clipboard"] = "4/20/2007 8:00 AM";
  PasteCellValue (Grid, ChildTable, 0, "ShippedDate");
}

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

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

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

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

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

See Also

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

Highlight search results