Copying and Pasting Cell Values in Microsoft DataGrid

Applies to TestComplete 15.46, last modified on January 09, 2023

When testing an application with Microsoft DataGrid .NET controls, 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 DataGrid 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.

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

When testing Microsoft DataGrid controls, use specific methods and properties of the corresponding MicrosoftDataGrid 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 DataGrid 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 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 and select it. The DataGrid control automatically starts editing the selected cell and selects its contents, so there is no need to perform additional actions to activate the edit mode and specify the cell contents to be copied or pasted.

To copy the cell value 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).

Below in an example that illustrates how you can copy DataGrid cell values to and paste them from the clipboard.

Example

View description

JavaScript, JScript

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

  // Obtain the grid object
  p = Sys.Process ("DataGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1");

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

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

  // Specify the clipboard contents directly
  Sys.Clipboard = "123412341234";
  PasteCellValue (Grid, 0, "Card No");
}

function CopyCellValue (Grid, RowIndex, ColumnId)
{
  Grid.ClickCell (RowIndex, ColumnId);
  Grid.Keys ("^c");
}

function PasteCellValue (Grid, RowIndex, ColumnId)
{
  Grid.ClickCell (RowIndex, ColumnId);
  Grid.Keys ("^v");
}

Python

def Main():

  # Obtain the grid object
  p = Sys.Process ("DataGridSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

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

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

  # Specify the clipboard contents directly
  Sys.Clipboard = "123412341234"
  PasteCellValue (Grid, 0, "Card No")

def CopyCellValue (Grid, RowIndex, ColumnId):
  Grid.ClickCell (RowIndex, ColumnId)
  Grid.Keys ("^c")

def PasteCellValue (Grid, RowIndex, ColumnId):
  Grid.ClickCell (RowIndex, ColumnId)
  Grid.Keys ("^v")

VBScript

Sub Main
  Dim p, Grid, i

  ' Obtain the grid object
  Set p = Sys.Process ("DataGridSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

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

  ' Paste the copied value to other cells
  For i = 1 To 5
    Call PasteCellValue (Grid, i, "Product")
  Next

  ' Specify the clipboard contents directly
  Sys.Clipboard = "123412341234"
  Call PasteCellValue (Grid, 0, "Card No")
End Sub

Sub CopyCellValue (Grid, RowIndex, ColumnId)
  Call Grid.ClickCell (RowIndex, ColumnId)
  Grid.Keys ("^c")
End Sub

Sub PasteCellValue (Grid, RowIndex, ColumnId)
  Call Grid.ClickCell (RowIndex, ColumnId)
  Grid.Keys ("^v")
End Sub

DelphiScript

procedure CopyCellValue (Grid, RowIndex, ColumnId);
begin
  Grid.ClickCell (RowIndex, ColumnId);
  Grid.Keys ('^c');
end;

procedure PasteCellValue (Grid, RowIndex, ColumnId);
begin
  Grid.ClickCell (RowIndex, ColumnId);
  Grid.Keys ('^v');
end;

procedure Main;
var p, Grid, i : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('DataGridSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGrid1');

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

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

  // Specify the clipboard contents directly
  Sys.Clipboard := '123412341234';
  PasteCellValue (Grid, 0, 'Card No');
end;

C++Script, C#Script

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

  // Obtain the grid object
  p = Sys["Process"]("DataGridSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGrid1");

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

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

  // Specify the clipboard contents directly
  Sys["Clipboard"] = "123412341234";
  PasteCellValue (Grid, 0, "Card No");
}

function CopyCellValue (Grid, RowIndex, ColumnId)
{
  Grid["ClickCell"](RowIndex, ColumnId);
  Grid["Keys"]("^c");
}

function PasteCellValue (Grid, RowIndex, ColumnId)
{
  Grid["ClickCell"](RowIndex, ColumnId);
  Grid["Keys"]("^v");
}

Copying Selected Records to the Clipboard

The DataGrid control lets the users copy contents of the selected cells to the clipboard, so that it can be easily used by other applications, such as Notepad, Microsoft Excel or Microsoft Word.

The selected records are copied to the clipboard when the user presses the Ctrl+C shortcut. You can simulate this key combination using the Keys action applied to the grid control. The following example illustrates how to do this.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

  // Obtain th grid object
  p = Sys.Process ("DataGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1");

  // Select the range of cells in the grid
  SelectRange (Grid, 2, 5);

  Grid.Keys ("^c");

  aqFile.WriteToTextFile ("C:\SelectedData.txt", Sys.Clipboard, aqFile.ctANSI, true);
  Log.File ("C:\SelectedData.txt");
}

function SelectRange (Grid, StartRowIndex, EndRowIndex)
{
  // Select the first row of the range
  Grid.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift);
}

Python

def Main():

  # Obtain th grid object
  p = Sys.Process ("DataGridSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  # Select the range of cells in the grid
  SelectRange (Grid, 2, 5)

  Grid.Keys ("^c")

  aqFile.WriteToTextFile ("C:\SelectedData.txt", Sys.Clipboard, aqFile.ctANSI, True)
  Log.File ("C:\SelectedData.txt")

def SelectRange (Grid, StartRowIndex, EndRowIndex):
  # Select the first row of the range
  Grid.ClickRowIndicator (StartRowIndex)
  # Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift)

VBScript

Sub Main
  Dim p, Grid

  ' Obtain th grid object
  Set p = Sys.Process ("DataGridSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  ' Select the range of cells in the grid
  Call SelectRange (Grid, 2, 5)

  Grid.Keys ("^c")

  Call aqFile.WriteToTextFile ("C:\SelectedData.txt", Sys.Clipboard, aqFile.ctANSI, True)
  Log.File ("C:\SelectedData.txt")
End Sub

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

DelphiScript

procedure SelectRange (Grid, StartRowIndex, EndRowIndex);
begin
  // Select the first row of the range
  Grid.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift);
end;

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain th grid object
  p := Sys.Process ('DataGridSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGrid1');

  // Select the range of cells in the grid
  SelectRange (Grid, 2, 5);

  Grid.Keys ('^c');

  aqFile.WriteToTextFile ('C:\SelectedData.txt', Sys.Clipboard, aqFile.ctANSI, true);
  Log.File ('C:\SelectedData.txt');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain th grid object
  p = Sys["Process"]("DataGridSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGrid1");

  // Select the range of cells in the grid
  SelectRange (Grid, 2, 5);

  Grid["Keys"]("^c");

  aqFile["WriteToTextFile"]("C:\\SelectedData.txt", Sys["Clipboard"], aqFile["ctANSI"], true);
  Log["File"]("C:\SelectedData.txt");
}

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

See Also

Working With Microsoft DataGrid
Selecting Cells in Microsoft DataGrid
Obtaining and Setting Cell Values in Microsoft DataGrid
Clipboard Property

Highlight search results