Copying and Pasting Cell Values in Microsoft DataGridView

Applies to TestComplete 15.69, last modified on November 13, 2024

When testing an application with the DataGridView 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 DataGridView 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 DataGridView control. For this purpose, the .NET Application Support and Microsoft Control Support plugins must be installed and enabled.

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

Below in an example that illustrates how you can copy DataGridView 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 ("DataGridViewSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1");

  // 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<6; i++)
    PasteCellValue (Grid, i, "Product");
}

// Copies the cell value to the clipboard
function CopyCellValue (Grid, RowIndex, ColumnId)
{
  // Select the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Select the cell contents and copy it to the clipboard
  Grid.Keys ("^a^c");
  CancelEditing (Grid);
}

// Copies the cell value from the clipboard
function PasteCellValue (Grid, RowIndex, ColumnId)
{
  // Select the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Select the cell contents and paste the new value from the clipboard
  Grid.Keys ("^a^v");
  // Save the changes made
  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 grid object
  p = Sys.Process ("DataGridViewSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  # 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, 6):
    PasteCellValue (Grid, i, "Product")

# Copies the cell value to the clipboard
def CopyCellValue (Grid, RowIndex, ColumnId):
  # Select the cell
  Grid.ClickCell (RowIndex, ColumnId)
  # Activate the edit mode
  ActivateCellEditor (Grid)
  # Select the cell contents and copy it to the clipboard
  Grid.Keys ("^a^c")
  CancelEditing (Grid)

# Copies the cell value from the clipboard
def PasteCellValue (Grid, RowIndex, ColumnId):
  # Select the cell
  Grid.ClickCell (RowIndex, ColumnId)
  # Activate the edit mode
  ActivateCellEditor (Grid)
  # Select the cell contents and paste the new value from the clipboard
  Grid.Keys ("^a^v")
  # Save the changes made
  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, Grid, i

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

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

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

' Copies the cell value to the clipboard
Sub CopyCellValue (Grid, RowIndex, ColumnId)
  ' Select the cell
  Call Grid.ClickCell (RowIndex, ColumnId)
  ' Activate the edit mode
  Call ActivateCellEditor (Grid)
  ' Select the cell contents and copy it to the clipboard
  Call Grid.Keys ("^a^c")
  Call CancelEditing (Grid)
End Sub

' Copies the cell value from the clipboard
Sub PasteCellValue (Grid, RowIndex, ColumnId)
  ' Select the cell
  Call Grid.ClickCell (RowIndex, ColumnId)
  ' Activate the edit mode
  Call ActivateCellEditor (Grid)
  ' Select the cell contents and paste the new value from the clipboard
  Call Grid.Keys ("^a^v")
  ' Save the changes made
  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, RowIndex, ColumnId); forward;
procedure PasteCellValue (Grid, RowIndex, ColumnId); forward;
procedure ActivateCellEditor (Grid); forward;
procedure CloseCellEditor (Grid); forward;
procedure CancelEditing (Grid); forward;

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

  // 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');
end;

// Copies the cell value to the clipboard
procedure CopyCellValue (Grid, RowIndex, ColumnId);
begin
  // Select the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Select the cell contents and copy it to the clipboard
  Grid.Keys ('^a^c');
  CancelEditing (Grid);
end;

// Copies the cell value from the clipboard
procedure PasteCellValue (Grid, RowIndex, ColumnId);
begin
  // Select the cell
  Grid.ClickCell (RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Select the cell contents and paste the new value from the clipboard
  Grid.Keys ('^a^v');
  // Save the changes made
  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, Grid, i;

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

  // 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<6; i++)
    PasteCellValue (Grid, i, "Product");
}

// Copies the cell value to the clipboard
function CopyCellValue (Grid, RowIndex, ColumnId)
{
  // Select the cell
  Grid["ClickCell"](RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Select the cell contents and copy it to the clipboard
  Grid["Keys"]("^a^c");
  CancelEditing (Grid);
}

// Copies the cell value from the clipboard
function PasteCellValue (Grid, RowIndex, ColumnId)
{
  // Select the cell
  Grid["ClickCell"](RowIndex, ColumnId);
  // Activate the edit mode
  ActivateCellEditor (Grid);
  // Select the cell contents and paste the new value from the clipboard
  Grid["Keys"]("^a^v");
  // Save the changes made
  CloseCellEditor (Grid);
}

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

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

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

Copying Selected Records to the Clipboard

The DataGridView 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 cells contents are copied to the clipboard in different formats: as tab- and comma-delimited text and as an HTML-formatted table. Depending on the grid’s ClipboardCopyMode property, the copied text may or may not include the row and column headers text:

  • If ClipboardCopyMode is Disable, the copying feature is disabled.
  • If ClipboardCopyMode is EnableAlwaysIncludeHeaderText, the copied text will include values of selected cells as well as captions of rows and columns that contain selected cells.
  • If ClipboardCopyMode is EnableWithAutoHeaderText, the copied text will include the values of selected cells. The row and column headers will be included in the copied text only if at least one header is selected.
  • If ClipboardCopyMode is EnableWithoutHeaderText, the copied text will include values of the selected cells without row and column headers.

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.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid, CopyModes, OldMode, i, FileName, FileExt;

  FileName = "C:\\GridData_"; FileExt = ".txt";
  CopyModes = new Array ("Disable", "EnableAlwaysIncludeHeaderText", "EnableWithAutoHeaderText", "EnableWithoutHeaderText");

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

  // Select the range of cells in the grid
  SelectCellRange (Grid, 1, 1, 6, 5);

  // Save the initial ClipboardCopyMode property value
  OldMode = Grid.ClipboardCopyMode;

  for (i in CopyModes)
  {
    // Clear the clipboard
    Sys.Clipboard = "";
    // Set the copy mode
    Grid.ClipboardCopyMode = CopyModes[i];
    // Copy selected cells to the clipboard
    Grid.Keys ("^C");
    // Save the clipboard contents to a text file
    aqFile.WriteToTextFile (FileName + CopyModes[i] + FileExt, Sys.Clipboard, aqFile.ctANSI, true);
  }
  
  // Restore the initial ClipboardCopyMode property value
  Grid.ClipboardCopyMode = OldMode;
}

// Selects a range of rows
function SelectRowRange (Grid, StartRowIndex, EndRowIndex)
{
  // Select the 1st row of the range
  Grid.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift);
}

Python

def Main ():

  FileName = "C:\\GridData_" 
  FileExt = ".txt"
  CopyModes = list("Disable", "EnableAlwaysIncludeHeaderText", "EnableWithAutoHeaderText", "EnableWithoutHeaderText")

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

  # Select the range of cells in the grid
  SelectCellRange (Grid, 1, 1, 6, 5)

  # Save the initial ClipboardCopyMode property value
  OldMode = Grid.ClipboardCopyMode

  for i in CopyModes: 
    # Clear the clipboard
    Sys.Clipboard = ""
    # Set the copy mode
    Grid.ClipboardCopyMode = CopyModes[i]
    # Copy selected cells to the clipboard
    Grid.Keys ("^C")
    # Save the clipboard contents to a text file
    aqFile.WriteToTextFile (FileName + CopyModes[i] + FileExt, Sys.Clipboard, aqFile.ctANSI, True)
  
  # Restore the initial ClipboardCopyMode property value
  Grid.ClipboardCopyMode = OldMode

# Selects a range of rows
def SelectRowRange (Grid, StartRowIndex, EndRowIndex):
  # Select the 1st row of the range
  Grid.ClickRowIndicator (StartRowIndex)
  # Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift)

VBScript

Sub Main
  Dim p, Grid, CopyModes, OldMode, Mode, FileName, FileExt

  FileName = "C:\GridData_"
  FileExt = ".txt"
  CopyModes = Array ("Disable", "EnableAlwaysIncludeHeaderText", "EnableWithAutoHeaderText", "EnableWithoutHeaderText")

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

  ' Select the range of cells in the grid
  Call SelectRowRange (Grid, 1, 6)

  ' Save the initial ClipboardCopyMode property value
  OldMode = Grid.ClipboardCopyMode

  For Each Mode In CopyModes
    ' Clear the clipboard
    Sys.Clipboard = ""
    ' Set the copy mode
    Grid.ClipboardCopyMode = Mode
    ' Copy selected records to the clipboard
    Call Grid.Keys ("^C")
    ' Save the clipboard contents to a text file
    Call aqFile.WriteToTextFile (FileName & Mode & FileExt, Sys.Clipboard, aqFile.ctANSI, True)
  Next

  ' Restore the initial ClipboardCopyMode property value
  Grid.ClipboardCopyMode = OldMode
End Sub

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

DelphiScript

procedure SelectRowRange (Grid, StartRowIndex, EndRowIndex); forward;

procedure Main;
var
  p, Grid, OldMode, i, FileName, FileExt : OleVariant;
  CopyModes : array [0..3];
begin
  FileName := 'C:\GridData_'; FileExt := '.txt';
  CopyModes[0] := 'Disable';
  CopyModes[1] := 'EnableAlwaysIncludeHeaderText';
  CopyModes[2] := 'EnableWithAutoHeaderText';
  CopyModes[3] := 'EnableWithoutHeaderText';

  // Obtain th grid object
  p := Sys.Process ('DataGridViewSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGridView1');

  // Select the range of cells in the grid
  SelectRowRange (Grid, 1, 6);

  // Save the initial ClipboardCopyMode property value
  OldMode := Grid.ClipboardCopyMode;

  for i := 0 to 3 do
  begin
    // Clear the clipboard
    Sys.Clipboard := '';
    // Set the copy mode
    Grid.ClipboardCopyMode := CopyModes[i];
    // Copy the selected records to the clipboard
    Grid.Keys ('^C');
    // Save the clipboard contents to a text file
    aqFile.WriteToTextFile (FileName + CopyModes[i] + FileExt, Sys.Clipboard, aqFile.ctANSI, true);
  end;

  // Restore the initial ClipboardCopyMode property value
  Grid.ClipboardCopyMode := OldMode;
end;

// Selects a range of rows
procedure SelectRowRange (Grid, StartRowIndex, EndRowIndex);
begin
  // Select the 1st row of the range
  Grid.ClickRowIndicator (StartRowIndex);
  // Extend the selection
  Grid.ClickRowIndicator (EndRowIndex, skShift);
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, CopyModes, OldMode, i, FileName, FileExt;

  FileName = "C:\\GridData_"; FileExt = ".txt";
  CopyModes = new Array ("Disable", "EnableAlwaysIncludeHeaderText", "EnableWithAutoHeaderText", "EnableWithoutHeaderText");

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

  // Select the range of cells in the grid
  SelectCellRange (Grid, 1, 1, 6, 5);

  // Save the initial ClipboardCopyMode property value
  OldMode = Grid["ClipboardCopyMode"];

  for (i in CopyModes)
  {
    // Clear the clipboard
    Sys["Clipboard"] = "";
    // Set the copy mode
    Grid["ClipboardCopyMode"] = CopyModes[i];
    // Copy selected cells to the clipboard
    Grid["Keys"]("^C");
    // Save the clipboard contents to a text file
    aqFile["WriteToTextFile"] (FileName + CopyModes[i] + FileExt, Sys["Clipboard"], aqFile["ctANSI"], true);
  }
  
  // Restore the initial ClipboardCopyMode property value
  Grid["ClipboardCopyMode"] = OldMode;
}

// Selects a range of rows
function SelectRowRange (Grid, StartRowIndex, EndRowIndex)
{
  // Select the 1st row of the range
  Grid["ClickRowIndicator"](StartRowIndex);
  // Extend the selection
  Grid["ClickRowIndicator"](EndRowIndex, skShift);
}

See Also

Working With Microsoft DataGridView
Selecting Cells in Microsoft DataGridView
Obtaining and Setting Cell Values in Microsoft DataGridView

Highlight search results