Copying and Pasting Cell Values in Infragistics UltraGrid

Applies to TestComplete 15.42, last modified on August 01, 2022

Copying and pasting cell values are typical operations that you will perform over grid controls. You can copy, cut and paste the cell values via the clipboard. With TestComplete, you can directly access the clipboard’s contents using the Sys.Clipboard property. Using the clipboard you can transfer data between different cells, grids, windows and even applications. This topic describes how you can copy and paste UltraGrid cell values from scripts.

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

When testing Infragistics UltraGrid controls, use specific methods and properties of the corresponding Infragistics UltraGrid 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 UltraGrid 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.

Before copying or pasting the cell’s value, you need to focus the cell. The easiest way to do it is to perform a mouse click over it with the ClickCell action. Then, you need to add the cell's contents that you want to copy. For example, to select the entire cell value, you can press the End key and than simulate the Shift + Home keystroke.

To copy the selection to the clipboard, you can use the Ctrl+C shortcut, or select the Copy command from the cell’s context menu. The shortcut approach is easier, and consists of using the Keys action to send the shortcut to the grid control. The context menu approach requires more coding. If you decide to use the context menu, you should first call it using the ClickCellR action, or by sending the Application key press to the grid. After the context menu is displayed on the screen, you can obtain it via the PopupMenu property of the cell editor object. Using the Click action of the obtained Menu object, you can simulate the selection of the desired menu item.

To paste the value from the clipboard, you should perform the same actions. The only difference is that you should simulate the Ctrl+V keystroke or choose the Paste command from the context menu.

Example

View description

JavaScript

function CopyCellValue(Grid, View, RowIndex, ColumnId)
{
  if (strictEqual(View , null))
    Grid.ClickCell(RowIndex, ColumnId);
  else
    View.ClickCell(RowIndex, ColumnId);
       
  
  // Select the contents of the cell
  // via the End and Shift-Home keystroke
  Grid.Keys("[End]![Home]");
  // Copy the cell's contents to clipboard
  // -- by simulating the Ctrl-C keystroke
  Grid.Keys("^c");
  // -- by choosing the context menu item
  //Grid.Keys("[Apps]");
  //Grid.PopupMenu.Click("Copy");

  return Sys.Clipboard;
}


function PasteValueToCell(Grid, View, RowIndex, ColumnId)
{
  if (strictEqual(View , null))
    Grid.ClickCell(RowIndex, ColumnId);
  else
    View.ClickCell(RowIndex, ColumnId);
       
  
  // Select the contents of the cell
  // via the End and Shift-Home keystroke
  Grid.Keys("[End]![Home]");
  // Copy the cell's contents to clipboard
  // -- by simulating the Ctrl-C keystroke
  //Grid.Keys("^v");
  // -- by choosing the context menu item
  Grid.Keys("[Apps]");
  Grid.PopupMenu.Click("Paste");
}

function CopyPasteSample()
{
var p, Grid, AView, str;

  // Obtain the application process
  p = Sys.Process("SamplesExplorer");
  // Select the "Fixed Headers" demo
  p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V3 Fixed Headers");
  // Obtain the grid control
  Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");
  // Obtain the InfragisticsWinGridView object that corresponds to the third row
  AView = Grid.wChildView(2);

  Grid.Expand(2);
  str = CopyCellValueExtended(Grid, AView, 0, 2);
  Log.Message("Clipboard contents: "+str);
  Sys.Clipboard = '567321';
  PasteValueToCell(Grid, AView, 0, 4);
  Grid.Collapse(2);
}

JScript

function CopyCellValue(Grid, View, RowIndex, ColumnId)
{
  if (View == null )
    Grid.ClickCell(RowIndex, ColumnId);
  else
    View.ClickCell(RowIndex, ColumnId);
       
  
  // Select the contents of the cell
  // via the End and Shift-Home keystroke
  Grid.Keys("[End]![Home]");
  // Copy the cell's contents to clipboard
  // -- by simulating the Ctrl-C keystroke
  Grid.Keys("^c");
  // -- by choosing the context menu item
  //Grid.Keys("[Apps]");
  //Grid.PopupMenu.Click("Copy");

  return Sys.Clipboard;
}


function PasteValueToCell(Grid, View, RowIndex, ColumnId)
{
  if (View == null )
    Grid.ClickCell(RowIndex, ColumnId);
  else
    View.ClickCell(RowIndex, ColumnId);
       
  
  // Select the contents of the cell
  // via the End and Shift-Home keystroke
  Grid.Keys("[End]![Home]");
  // Copy the cell's contents to clipboard
  // -- by simulating the Ctrl-C keystroke
  //Grid.Keys("^v");
  // -- by choosing the context menu item
  Grid.Keys("[Apps]");
  Grid.PopupMenu.Click("Paste");
}

function CopyPasteSample()
{
var p, Grid, AView, str;

  // Obtain the application process
  p = Sys.Process("SamplesExplorer");
  // Select the "Fixed Headers" demo
  p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V3 Fixed Headers");
  // Obtain the grid control
  Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1");
  // Obtain the InfragisticsWinGridView object that corresponds to the third row
  AView = Grid.wChildView(2);

  Grid.Expand(2);
  str = CopyCellValueExtended(Grid, AView, 0, 2);
  Log.Message("Clipboard contents: "+str);
  Sys.Clipboard = '567321';
  PasteValueToCell(Grid, AView, 0, 4);
  Grid.Collapse(2);
}

Python

def CopyCellValue(Grid, View, RowIndex, ColumnId):
  if (View == None ):
    Grid.ClickCell(RowIndex, ColumnId)
  else:
    View.ClickCell(RowIndex, ColumnId)
       
  
  # Select the contents of the cell
  # via the End and Shift-Home keystroke
  Grid.Keys("[End]![Home]")
  # Copy the cell's contents to clipboard
  # -- by simulating the Ctrl-C keystroke 
  Grid.Keys("^c")
  # -- by choosing the context menu item
  #Grid.Keys("[Apps]")
  #Grid.PopupMenu.Click("Copy")

  return Sys.Clipboard 


def PasteValueToCell(Grid, View, RowIndex, ColumnId):
  if (View == None ):
    Grid.ClickCell(RowIndex, ColumnId)
  else:
    View.ClickCell(RowIndex, ColumnId)
       
  
  # Select the contents of the cell
  # via the End and Shift-Home keystroke
  Grid.Keys("[End]![Home]")
  # Copy the cell's contents to clipboard
  # -- by simulating the Ctrl-C keystroke 
  #Grid.Keys("^v")
  # -- by choosing the context menu item
  Grid.Keys("[Apps]")
  Grid.PopupMenu.Click("Paste") 

def CopyPasteSample():

  # Obtain the application process
  p = Sys.Process("SamplesExplorer")
  # Select the "Fixed Headers" demo
  p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V3 Fixed Headers")
  # Obtain the grid control
  Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")
  # Obtain the InfragisticsWinGridView object that corresponds to the third row
  AView = Grid.wChildView[2]

  Grid.Expand(2)
  str = CopyCellValueExtended(Grid, AView, 0, 2)
  Log.Message("Clipboard contents: "+str)
  Sys.Clipboard = '567321'
  PasteValueToCell(Grid, AView, 0, 4) 
  Grid.Collapse(2)

VBScript

Function CopyCellValue(Grid, View, RowIndex, ColumnId)

  If View is Nothing Then
    Call Grid.ClickCell(RowIndex, ColumnId)
  Else
    Call View.ClickCell(RowIndex, ColumnId)
  End If
  
  ' Select the contents of the cell
  ' via the End and Shift-Home keystroke
  Call Grid.Keys("[End]![Home]")
  ' Copy the cell's contents to clipboard
  ' -- by simulating the Ctrl-C keystroke
  Call Grid.Keys("^c")
  ' -- by choosing the context menu item
  'Call Grid.Keys("[Apps]")
  'Call Grid.PopupMenu.Click("Copy")

  CopyCellValue = Sys.Clipboard
End Function

Function PasteValueToCell(Grid, View, RowIndex, ColumnId)

  If View is Nothing Then
    Call Grid.ClickCell(RowIndex, ColumnId)
  Else
    Call View.ClickCell(RowIndex, ColumnId)
  End If
  
  ' Select the contents of the cell
  ' via the End and Shift-Home keystroke
  Call Grid.Keys("[End]![Home]")
  ' Copy the cell's contents to clipboard
  ' -- by simulating the Ctrl-C keystroke
  Call Grid.Keys("^v")
  ' -- by choosing the context menu item
  'Call Grid.Keys("[Apps]")
  'Call Grid.PopupMenu.Click("Paste")
End Function

Sub CopyPasteSample
Dim p, Grid, AView

  ' Obtain the application process
  Set p = Sys.Process("SamplesExplorer")
  ' Select the "Fixed Headers" demo
  Call p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V3 Fixed Headers")
  ' Obtain the grid control
  Set Grid = Sys.Process("SamplesExplorer").WinFormsObject("frmFixedHeaders").WinFormsObject("UltraGrid1")
  ' Obtain the InfragisticsWinGridView object that corresponds to the third row
  Set AView = Grid.wChildView(2)

  Call Grid.Expand(2)
  str = CopyCellValueExtended(Grid, AView, 0, 2)
  Log.Message("Clipboard contents: "+str)
  Sys.Clipboard = "567321"
  Call PasteValueToCell(Grid, AView, 0, 4)
  Call Grid.Collapse(2)
End Sub

DelphiScript

function CopyCellValue(Grid, View, RowIndex, ColumnId): Variant;
begin
  if (View = nil) then
    Grid.ClickCell(RowIndex, ColumnId)
  else
    View.ClickCell(RowIndex, ColumnId);
         
  
  // Select the contents of the cell
  // via the End and Shift-Home keystroke
  Grid.Keys('[End]![Home]');
  // Copy the cell's contents to clipboard
  // -- by simulating the Ctrl-C keystroke
  Grid.Keys('^c');
  // -- by choosing the context menu item
  //Grid.Keys('[Apps]');
  //Grid.PopupMenu.Click('Copy');

  Result := Sys.Clipboard;
end;

procedure PasteValueToCell(Grid, View, RowIndex, ColumnId);
begin
  if (View = nil) then
    Grid.ClickCell(RowIndex, ColumnId)
  else
    View.ClickCell(RowIndex, ColumnId);
         
  
  // Select the contents of the cell
  // via the End and Shift-Home keystroke
  Grid.Keys('[End]![Home]');
  // Copy the cell's contents to clipboard
  // -- by simulating the Ctrl-C keystroke
  Grid.Keys('^v');
  // -- by choosing the context menu item
  //Grid.Keys('[Apps]');
  //Grid.PopupMenu.Click('Paste');
end;

procedure CopyPasteSample;
var p, Grid, AView, str: OleVariant;
begin

  // Obtain the application process
  p := Sys.Process('SamplesExplorer');
  // Select the "Fixed Headers" demo
  p.frmMain.WinFormsObject('tvwSamples').DblClickItem('|Feature Samples|V3 Fixed Headers');
  // Obtain the grid control
  Grid := Sys.Process('SamplesExplorer').WinFormsObject('frmFixedHeaders').WinFormsObject('UltraGrid1');
  // Obtain the InfragisticsWinGridView object that corresponds to the third row
  AView := Grid.wChildView(2);

  Grid.Expand(2);
  str := CopyCellValueExtended(Grid, AView, 0, 2);
  Log.Message('Clipboard contents: '+str);
  Sys.Clipboard := '567321';
  PasteValueToCell(Grid, AView, 0, 4);
  Grid.Collapse(2);
end;

C++Script, C#Script

function CopyCellValue(Grid, View, RowIndex, ColumnId)
{
  if (View == null )
    Grid["ClickCell"](RowIndex, ColumnId);
  else
    View["ClickCell"](RowIndex, ColumnId);
       
  
  // Select the contents of the cell
  // via the End and Shift-Home keystroke
  Grid["Keys"]("[End]![Home]");
  // Copy the cell's contents to clipboard
  // -- by simulating the Ctrl-C keystroke
  Grid["Keys"]("^c");
  // -- by choosing the context menu item
  //Grid["Keys"]("[Apps]");
  //Grid["PopupMenu"]["Click"]("Copy");

  return Sys["Clipboard"];
}

function PasteValueToCell(Grid, View, RowIndex, ColumnId)
{
  if (View == null )
    Grid["ClickCell"](RowIndex, ColumnId);
  else
    View["ClickCell"](RowIndex, ColumnId);
       
  
  // Select the contents of the cell
  // via the End and Shift-Home keystroke
  Grid["Keys"]("[End]![Home]");
  // Copy the cell's contents to clipboard
  // -- by simulating the Ctrl-C keystroke
  Grid["Keys"]("^v");
  // -- by choosing the context menu item
  //Grid["Keys"]("[Apps]");
  //Grid["PopupMenu"]["Click"]("Paste");
}

function CopyPasteSample()
{
var p, Grid, AView, str;

  // Obtain the application process
  p = Sys["Process"]("SamplesExplorer");
  // Select the "Fixed Headers" demo
  p["frmMain"]["WinFormsObject"]("tvwSamples")["DblClickItem"]("|Feature Samples|V3 Fixed Headers");
  // Obtain the grid control
  Grid = Sys["Process"]("SamplesExplorer")["WinFormsObject"]("frmFixedHeaders")["WinFormsObject"]("UltraGrid1");
  // Obtain the InfragisticsWinGridView object that corresponds to the third row
  AView = Grid["wChildView"](2);

  Grid["Expand"](2);
  str = CopyCellValueExtended(Grid, AView, 0, 2);
  Log["Message"]("Clipboard contents: "+str);
  Sys["Clipboard"] = '567321';
  PasteValueToCell(Grid, AView, 0, 4);
  Grid["Collapse"](2);
}

See Also

Working With Infragistics UltraGrid
Accessing Grid Elements in Infragistics UltraGrid
Working With Specific In-place Editors in Infragistics UltraGrid
Obtaining and Setting Cell Values in Infragistics UltraGrid
Clipboard Property

Highlight search results