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

Clipboard - Copy and Paste Operations. Data Formatting

  • 7 minutes to read

Copy Data

Certain DevExpress controls allow end-users to copy a selection to the Clipboard using the CTRL+C shortcut. These controls also provide the CopyToClipboard method to copy a selection programmatically.

The Data Grid control also provides additional options to control data copying operations. These options are accessible from the GridView.OptionsClipboard object.

The following additional Clipboard options are accessible from the Tree List control’s TreeList.OptionsClipboard object:

Copy Formatting

The Copy Formatting feature, supported by the WinForms and WPF Data Grid and Tree List controls, allows end-users to copy the control’s selection along with formatting settings (fonts, cell background and foreground) to the Clipboard in RTF, HTML and Biff8 (XLS) formats. When pasting this data in MS Word, MS Excel or MS Outlook, the data retains its original formatting.

Clipboard Manager

To enable the Copy Formatting feature, set the control’s ClipboardMode option (ClipboardOptions.ClipboardMode) to Formatted. This option is accessible from the GridView.OptionsClipboard and TreeList.OptionsClipboard properties for WinForms Grid and TreeList controls.

In Formatted data copy mode, the selection is copied in multiple formats simultaneously: RTF, HTML, XLS (Biff8), CSV, UnicodeText, and Text. You can disable specific formats (for example, to improve the application’s performance) using the control’s OptionsClipboard object:

The following are additional settings supported in Formatted data copy mode:

Note

The WinForms Data Grid supports the Copy Formatting feature for the GridView, BandedGridView, and AdvBandedGridView.

Paste Data

From version 17.2, end-users can use the CTRL+V and SHIFT+INSERT keyboard combinations to paste plain and formatted data in the Data Grid (GridView and BandedGridView) and Tree List controls in the following formats:

  • Biff8 (MS Excel)
  • Comma-separated values (CSV)
  • XML Spreadsheet
  • ANSI and Unicode text format. TAB characters separate column values; end-of-line characters separate rows.

To enable pasting, set the ClipboardOptions.PasteMode property (available from GridView.OptionsClipboard/TreeList.OptionsClipboard) to Append or Update:

  • Append - When pasting data, new rows with the clipboard values are added to the control. For bound controls, the new rows are added to the data source and the control is updated. The data source should support adding new rows. Otherwise, the paste operation fails.
  • Update - The pasted data updates the rectangular cell block whose upper left corner is the currently focused cell. Cells in the columns to the left of the focused column are not updated.

    The following animation shows which cells are updated when pasting two rows of text in the grid when the second column is focused. The TAB character separates pasted text items which can be converted to a numeric type.

    Paste-BlockSelection-TabbedNumbers

    Paste-BlockSelection.gif

    Note that cells in the first visible column are not updated during pasting.

Data Pasting Specifics

Customize Paste Operations

You can handle the following events to override the default pasting behavior and perform custom actions:

These events repeatedly fire for each pasted row and provide the following arguments to identify and modify the pasted data and cancel certain paste operations:

  • Values - Returns a dictionary that contains “target column - pasted value” pairs. You can modify pasted values in this dictionary to perform custom pasting.
  • OriginalValues - Returns a read-only collection of individual pasted values.
  • IsRowValid, IsNodeValid, and IsValueValid - Boolean methods that allow you to check data validity.
  • GetValidValues, GetInvalidValues - Return valid and invalid values.
  • PasteMode - Gets or sets whether only valid rows or all rows are pasted to the control.
  • Cancel - Gets or sets whether the current operation should be canceled.

In the following sample, “Yes” and “No” string values are pasted to a Boolean Grid column. This causes the e.IsRowValid method to return false and the e.GetInvalidRow method to return invalid values. The e.Values collection allows you to replace these strings with true and false values.


void gridView1_ClipboardRowPasting(object sender, DevExpress.XtraGrid.Views.Grid.ClipboardRowPastingEventArgs e) {
    GridView view = sender as GridView;
    GridColumn column = view.Columns["State"];

    if (!e.IsRowValid()) {
        if (object.Equals(e.Values["State"], "Yes")) e.Values[column] = true;
        else e.Values[column] = false;
    }

    // or

    if (e.GetInvalidValues().ContainsKey(column)) {
        if (object.Equals(e.Values["State"], "Yes")) e.Values[column] = true;
        else e.Values[column] = false;
    }
}

You can also handle this event to modify pasted values when they are valid. The sample below removes underscore characters from pasted strings and applies the “camel case” formatting to them.


void gridView1_ClipboardRowPasting(object sender, DevExpress.XtraGrid.Views.Grid.ClipboardRowPastingEventArgs e) {
    GridView view = sender as GridView;
    GridColumn column = view.Columns["Department"];

    string pastedString = e.Values[column].ToString();
    string newString = pastedString.Replace('_', ' ');
    string newCapitalizedString = Regex.Replace(newString, @"(^\w)|(\s\w)", m => m.Value.ToUpper());
    e.Values[column] = newCapitalizedString;
}

Note

Related API * ClipboardOptions.PasteMode - Gets or sets data pasting mode.

Demos