GridView.ClipboardRowPasting Event

Fires before a data row is pasted to the control. Allows you to apply a format, update the pasted data, or skip a data row.

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v20.1.dll

Declaration

[DXCategory("Action")]
public event ClipboardRowPastingEventHandler ClipboardRowPasting
<DXCategory("Action")>
Public Event ClipboardRowPasting As ClipboardRowPastingEventHandler

Event Data

The ClipboardRowPasting event's data class is ClipboardRowPastingEventArgs. The following properties provide information specific to this event:

Property Description
Cancel Gets or sets whether the current operation should be canceled.
DataRowCount Gets the count of rows that contain data to process (without column and band header rows).
OriginalValues Returns a read-only collection of individual pasted values.
PasteMode Gets or sets whether only valid rows or all rows are pasted to the control.
RowCount Gets the total count of rows to process (with column and band header rows).
RowHandle Returns the unique identifier (handle) of the currently processed row.
Values Returns a dictionary that contains "target column - pasted value" pairs. You can modify pasted values in this dictionary to perform custom pasting.

Remarks

The ClipboardRowPasting event fires before a row is pasted to the control from the clipboard. If the clipboard contains multiple rows, the event fires for each row. The event allows you to modify data or to cancel pasting the processed row.

A row is not pasted to the control if any of its columns cannot accept data being pasted. For example, if data types do not match. You can set the PasteMode event argument to Force to forcibly paste the row.

To check if a particular value or the entire row is valid, use the IsValueValid and IsRowValid methods.

See Clipboard for more information.

Examples

If a cell value that is about to be pasted is not valid, you can replace it with a valid value. The code below shows how to handle the ClipboardRowPasting event to replace the Yes and No string values with the True and False boolean values.

Use the IsValueValid and IsRowValid methods to check if a particular value or row is valid.


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 the ClipboardRowPasting event to modify pasted values even if 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;
}
See Also