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.v24.1.dll
NuGet Packages: DevExpress.Win.Grid, DevExpress.Win.Navigation
Declaration
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. |
The event data class exposes the following methods:
Method | Description |
---|---|
GetInvalidValues() | Returns a dictionary that contains invalid column-value pairs. Invalid pairs identify values that cannot be accepted by corresponding columns. |
GetValidValues() | Returns a dictionary that contains valid column-value pairs. Valid pairs identify values that can be accepted by corresponding columns. |
IsRowValid() | Returns whether pasted values can be accepted by target columns. Both the pasted values and target columns are specified by the ClipboardRowPastingEventArgs.Values dictionary. |
IsValueValid(GridColumn, Object, out Exception) | Returns whether the specified value can be pasted to the specified column cell. |
IsValueValid(String, Object, out Exception) | Returns whether the specified value can be pasted to the column with the specified field name. |
Remarks
The GridView fires the ClipboardRowPasting
event before a row is pasted to the control from the clipboard. If the clipboard contains multiple rows, the event fires for each row. Handle this event to modify data or to cancel pasting the 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.
Use the IsValueValid and IsRowValid methods to check if a particular value or the entire row is valid.
Read the following topic for detailed information: Clipboard.
Note
The ClipboardRowPasting
event fires if the PasteMode property is set to PasteMode.Append
or PasteMode.Update
.
Example
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;
}