Copying and Pasting DataGridView Cell Values

  February 27, 2007

It's often helpful to use the clipboard to copy/paste grid data when
testing applications with the DataGridView control. The DataGridView
control can also be configured to let the user copy the contents of
selected records to the clipboard so that they can be easily used by
other applications, such as Notepad, Microsoft Excel or Microsoft Word.
You can use this feature to organize a data exchange between the tested
application and another application.

Copying Selected Records to the Clipboard

The DataGridView control allows users copy contents of the selected cells to the clipboard. The cell's 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 shortcut using the Keys action. The following example illustrates how to do this. It copies values of selected cells to the clipboard in all possible selection modes and saves the copied text to a text file:

Copying and Pasting Cell Values

When editing grid data, you may need to insert the same values in multiple cells. You can do this by copying the value of a particular cell and pasting it to desired cells. You may also want to paste values that were previously copied to the clipboard from another application.

To be able to copy or paste a cell value, first, you need to select the desired cell and activate its in-place editor. I have already explained how you can do this in Selecting Cells in Microsoft DataGridView and Working With In-place Editors in Microsoft DataGridView.

After the cell is selected, you need to specify the cell contents to be copied or replaced. Usually, after activating the in-place editor, the cell’s data is selected, so there is no need to simulate the corresponding user actions. However, if the cell contents are not selected for some reason, you will need to select them. For example, to select the entire cell contents, you can simulate the Ctrl+A shortcut using the Keys action.

To copy the selection to the clipboard or replace the selection with the clipboard contents, you can use the Ctrl+C or Ctrl+V shortcut respectively, or select the corresponding command – Copy or Paste – from the cell’s context menu. The context menu approach is more complex, and it requires more coding, so I won't describe it here.

The example below demonstrates how you can copy and paste cell values by sending the Ctrl+C and Ctrl+V shortcuts to the grid control. The example contains the following routines:

  • Main is the main routine. It obtains the scripting object corresponding to the grid, calls CopyValue to copy the cell value, posts the clipboard contents to the log and calls the PasteValue routine to paste the copied value to some other cells.
  • CopyValue and PasteValue are used to copy the cell value to the clipboard and paste the clipboard contents to the cell respectively.

    To simulate a click on the grid cell, they call the helper ClickCell routine. You can find the code and description of this routine in Selecting Cells in Microsoft DataGridView. If you're going to try out the sample script below, add the ClickCell and GetCell routines from that blog post to your script.