Edit this page

Copy/Paste/Cut

RadGridView supports built-in Copy/Paste functionality, which allows you to store text in the Clipboard and then paste it in a different location. Using "Copy" and "Paste" gets quite useful when you want to enter the same content repeatedly.

Copying

The copying functionality in RadGridView is controlled via the ClipboardCopyMode property. It has three possible values:

  • Disable: Copying will not be permitted.

  • EnableWithoutHeaderText: Will copy the cells content, skipping any header cells data.

  • EnableAlwaysIncludeHeaderText will copy the cells content including the header cells data.

Copying is a pretty simple operation. After cell/row is selected, right click over the data cell/row opens a context menu where the copy option exists. After selecting it, you can paste the content anywhere you need to (in Notepad or Excel for example).

important

Since R1 2017 when you press Ctrl+C RadGridView copies the selected data considering the SelectionMode. If the SelectionMode property is set to FullRowSelect, pressing Ctrl+C will copy the entire row. If it is set to CellSelect only the selected cell will be copied when pressing Ctrl+C. Note that you can still copy just a single cell when the SelectionMode is set to FullRowSelect. It is necessary to select the desired cell and right click to open the context menu where you have "Copy" option.

Figure 1: Copy rows from RadGridView.

gridview-copy-paste 001

RadGridView introduces Copying event which occurs when the grid has prepared appropriate data formats that represent the copy selection. This event is fired once for each supported format:Text, HTML, CommaSeparatedValue. You can cancel this event if the data is not allowed to be stored to Clipboard in a specific format, e.g. HTML format:

private void radGridView1_Copying(object sender, GridViewClipboardEventArgs e)
{
    if (e.Format == DataFormats.Html)
    {
        e.Cancel = true;
    }
}

Private Sub radGridView1_Copying(sender As Object, e As GridViewClipboardEventArgs)
    If e.Format = DataFormats.Html Then
        e.Cancel = True
    End If
End Sub

note

Additionally, you can use the RadGridView.Copy method in order to perform programmatically copy functionality.

The CopyingCellClipboardContent event

The CopyingCellClipboardContent event is fired before each cell is copied. It allows you to easily change the copied values. The bellow example demonstrates how you can format the value in a particular cell before it is copied:

Copy only the time from a DateTime value.

private void RadGridView1_CopyingCellClipboardContent(object sender, GridViewCellValueEventArgs e)
{
    if (e.Value is DateTime)
    {
        var value = (DateTime)e.Value;
        e.Value = value.ToShortTimeString();
    }
}

Private Sub RadGridView1_CopyingCellClipboardContent(ByVal sender As Object, ByVal e As GridViewCellValueEventArgs)
    If TypeOf e.Value Is Date Then
        Dim value = CDate(e.Value)
        e.Value = value.ToShortTimeString()
    End If
End Sub

Pasting

The pasting functionality in RadGridView is controlled via the ClipboardPasteMode property. It has three possible modes:

  • Disable: Pasting is disabled.

  • Enable: Pasting is enabled

  • EnableWithNotifications: Pasting is enabled, and the respective cell events will be triggered upon paste operation.

The default context menu for data cells offers paste possibility, except when the RadGridView is read-only, disabled or the ClipboardPasteMode property is set to GridViewClipboardPasteMode.Disable.

Figure 2: Paste rows to RadGridView

gridview-copy-paste 002

RadGridView.Pasting event is appropriate for modifying the Clipboard data before pasting it in the grid.

The following example demonstrates how to capitalize the copied string before inserting it in the grid:

private void radGridView1_Pasting(object sender, GridViewClipboardEventArgs e)
{
    if (Clipboard.ContainsData(DataFormats.Text))
    {
        string data = Clipboard.GetData(DataFormats.Text).ToString();
        if (data != string.Empty)
        {
            Clipboard.SetData(DataFormats.Text, data.ToUpper());
        }
    }
}

Private Sub radGridView1_Pasting(sender As Object, e As GridViewClipboardEventArgs)
    If Clipboard.ContainsData(DataFormats.Text) Then
        Dim data As String = Clipboard.GetData(DataFormats.Text).ToString()
        If data <> String.Empty Then
            Clipboard.SetData(DataFormats.Text, data.ToUpper())
        End If
    End If
End Sub

You can cancel this event as well in order to prevent pasting data in some cases.

note

Additionally, you can use the RadGridView.Paste method in order to perform programmatically paste functionality.

PastingCellClipboardContent event

This event will be fired before the data is pasted in each cell. It allows you change or validate the data before the pasting operation is completed. The following example shows how you can show a message when invalid data is pasted.

Check the values when pasting

private void RadGridView1_PastingCellClipboardContent(object sender, GridViewCellValueEventArgs e)
{
    if (e.Column.Name == "Date")
    {
        DateTime date;
        var valid = DateTime.TryParse(e.Value.ToString(), out date);
        if (!valid)
        {
            RadMessageBox.Show("Invalid Date format pasted in the Date column");
            e.Value = DateTime.Now;
        }
    }
}

Private Sub RadGridView1_PastingCellClipboardContent(ByVal sender As Object, ByVal e As GridViewCellValueEventArgs)
    If e.Column.Name = "Date" Then
        Dim [date] As Date = Nothing
        Dim valid = Date.TryParse(e.Value.ToString(), [date])
        If Not valid Then
            RadMessageBox.Show("Invalid Date format pasted in the Date column")
            e.Value = Date.Now
        End If
    End If
End Sub

Key Combinations

Ctrl+C and Ctrl+V are the keys combinations, replacing Copy and Paste behavior. If the RadGridView.MultiSelect property is set to true, it is possible to select all the cells by pressing Ctrl+A. Afterwards, pressing Ctrl+C will copy all the cells’ content and it is ready to be pasted.

Cutting

The cutting functionality in RadGridView is controlled via the ClipboardCutMode property. It has three possible values:

  • Disable – cutting will not be permitted

  • EnableWithoutHeaderText – will cut the cells content, skipping any header cells data

  • EnableAlwaysIncludeHeaderText – will cut the cells content including the header cells data

Ctrl+X is the key combination, replacing Cut behavior.

note

Additionally, you can use the RadGridView.Cut method in order to perform programmatically cut functionality.

See Also