Skip to main content
A newer version of this page is available. .

Paste Operations

  • 3 minutes to read

You and your end-users can paste data from the Clipboard in the GridControl. This topic consists of the following sections:

Pasting At Runtime

Starting from version 17.2, end-users can paste plain and formatted data in the GridControl using the Ctrl+V or Shift+Insert shortcuts:

Paste operations have the following specifics:

  • If a target column cannot accept any value in a pasted row, this row is skipped.
  • Pasting graphics data is not supported.
  • Read-only and non-editable columns are skipped during pasting.
  • Adding new rows is disabled if the data source does not allow adding new rows.

Appending Data

You can use paste operations to add new rows to the GridControl. In this case, the pasted data is added as new rows to the data source, and the GridControl is updated. The data source has to support adding new rows to allow pasting.

In the following image, the paste operation adds data from Excel to the GridControl:

ClipboardPastingAppend

Set the TableView.PasteMode (or TreeListView.PasteMode) property to Append to allow pasting:

<dxg:GridControl>
   <dxg:GridControl.View>
      <dxg:TableView PasteMode="Append" />
   </dxg:GridControl.View>
</dxg:GridControl>

Updating Data

You can use pasting to update existing GridControl’s cells. In the following image, the paste operation updates GridControl’s data by data from the Excel:

ClipboardPastingUpdate

Set the TableView.PasteMode (or TreeListView.PasteMode) property to Update to allow pasting:

<dxg:GridControl>
   <dxg:GridControl.View>
      <dxg:TableView PasteMode="Update" />
   </dxg:GridControl.View>
</dxg:GridControl>

Pasting In Code

You can use the TableView.OnPaste (or TreeListView.OnPaste) method to paste data from the clipboard in the GridControl. The following code sample demonstrates how to update the required row with the clipboard’s data:

<dxg:GridControl>
   <dxg:GridControl.View> 
      <dxg:TableView Name="view" PasteMode="Update" /> 
   </dxg:GridControl.View> 
</dxg:GridControl>

<Button Click="OnClick">Paste from clipboard</Button>
void OnClick(object sender, RoutedEventArgs e) {
   view.FocusedRowHandle = 5;
   view.OnPaste();
}

Processing Paste Operations

The GridControl provides the following events that allow you to process paste operations manually. Handle these events to override the default pasting behavior and perform custom actions:

Event Description
TableView.ClipboardRowCellValuePasting (or TreeListView.ClipboardRowCellValuePasting) Raises for each pasted cell before updating the GridControl. Use this event to modify pasted data or to cancel pasting.
TableView.ClipboardRowPasting (or TreeListView.ClipboardRowPasting) Raises for each pasted row before updating the GridControl. Use this event to modify pasted data or to cancel pasting.
DataControlBase.PastingFromClipboard Raises after an end-user pressed Ctrl+V or Shift+Ins. Use this event to implement custom data pasting.

The following code sample demonstrates how to use the TableView.ClipboardRowCellValuePasting event to modify pasted values. This sample removes underscore characters from pasted strings and applies “camel case” formatting:

<dxg:GridControl>
   <dxg:GridControl.View>
      <dxg:TableView PasteMode="Update" ClipboardRowCellValuePasting="OnClipboardRowCellValuePasting" />
   </dxg:GridControl.View>
</dxg:GridControl>
void OnClipboardRowCellValuePasting(object sender, ClipboardRowCellValuePastingEventArgs e) {
   if(e.Column.FieldName == "Group") {
      string pastedString = e.Value.ToString();
      string newString = pastedString.Replace('_', ' ');
      string newCapitalizedString = Regex.Replace(newString, @"(^\w)|(\s\w)", m => m.Value.ToUpper());
      e.Value = newString;
   }
}