Paste Operations

  • 3 minutes to read

You and your end users can paste data from the Clipboard in the GridControl.

Paste at Runtime

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 in the Update paste mode.
  • Adding new rows is disabled if the data source does not allow adding new rows.

Append 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>

Update 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>

Paste 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();
}

Process 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 / TreeListView.ClipboardRowCellValuePasting Raises for each pasted cell before updating the GridControl. Use this event to modify pasted data or to cancel pasting.
TableView.ClipboardRowPasting / 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;
   }
}