Back to Devexpress

GridView.ClipboardRowPasting Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-e807194b.md

latest8.9 KB
Original Source

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.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Action")]
public event ClipboardRowPastingEventHandler ClipboardRowPasting
vb
<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:

PropertyDescription
CancelGets or sets whether the current operation should be canceled.
DataRowCountGets the count of rows that contain data to process (without column and band header rows).
OriginalValuesReturns a read-only collection of individual pasted values.
PasteModeGets or sets whether only valid rows or all rows are pasted to the control.
RowCountGets the total count of rows to process (with column and band header rows).
RowHandleReturns the unique identifier (handle) of the currently processed row.
ValuesReturns 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:

MethodDescription
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.

csharp
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;
    }
}
vb
Private Sub gridView1_ClipboardRowPasting(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.ClipboardRowPastingEventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    Dim column As GridColumn = view.Columns("State")

    If Not e.IsRowValid() Then
        If Object.Equals(e.Values("State"), "Yes") Then
          e.Values(column) = True
        Else
          e.Values(column) = False
        End If
    End If

    ' or

    If e.GetInvalidValues().ContainsKey(column) Then
        If Object.Equals(e.Values("State"), "Yes") Then
          e.Values(column) = True
        Else
          e.Values(column) = False
        End If
    End If
End Sub

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.

csharp
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;
}
vb
Private Sub gridView1_ClipboardRowPasting(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.ClipboardRowPastingEventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    Dim column As GridColumn = view.Columns("Department")

    Dim pastedString As String = e.Values(column).ToString()
    Dim newString As String = pastedString.Replace("_"c, " "c)
    Dim newCapitalizedString As String = Regex.Replace(newString, "(^\w)|(\s\w)", Function(m) m.Value.ToUpper())
    e.Values(column) = newCapitalizedString
End Sub

See Also

PasteMode

PasteFromClipboard()

ClipboardRowCopying

Clipboard

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace