DataGridView.GetClipboardContent Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Retrieves the formatted values that represent the contents of the selected cells for copying to the Clipboard.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Overridable Function GetClipboardContent As DataObject

Return Value

Type: System.Windows.Forms.DataObject

A DataObject that represents the contents of the selected cells.

This method retrieves data that represents the region defined by the selected cells. This region is the smallest rectangle that includes all of the selected cells. The value for each selected cell in this region is retrieved by calling the DataGridViewCell.GetClipboardContent method. Blank placeholder values are used for unselected cells in this region. This method combines these values into a DataObject containing several formats for copying to the clipboard. The supported clipboard formats include DataFormats.Text, DataFormats.UnicodeText, DataFormats.Html, and DataFormats.CommaSeparatedValue.

For more information, see the Clipboard class.

Notes to Inheritors:

Override this method to provide customized clipboard values. This is useful, for example, to support copying values from custom cell types.

The following code example demonstrates how to programmatically add selected DataGridView content to the Clipboard. This example is part of a larger example available in How to: Enable Users to Copy Multiple Cells to the Clipboard from the Windows Forms DataGridView Control.

Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Load

    ' Initialize the DataGridView control.
    Me.DataGridView1.ColumnCount = 5
    Me.DataGridView1.Rows.Add(New String() {"A", "B", "C", "D", "E"})
    Me.DataGridView1.Rows.Add(New String() {"F", "G", "H", "I", "J"})
    Me.DataGridView1.Rows.Add(New String() {"K", "L", "M", "N", "O"})
    Me.DataGridView1.Rows.Add(New String() {"P", "Q", "R", "S", "T"})
    Me.DataGridView1.Rows.Add(New String() {"U", "V", "W", "X", "Y"})
    Me.DataGridView1.ClipboardCopyMode = _

End Sub

Private Sub CopyPasteButton_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles CopyPasteButton.Click

    If Me.DataGridView1.GetCellCount( _
        DataGridViewElementStates.Selected) > 0 Then


            ' Add the selection to the clipboard.
            Clipboard.SetDataObject( _

            ' Replace the text box contents with the clipboard text.
            Me.TextBox1.Text = Clipboard.GetText()

        Catch ex As System.Runtime.InteropServices.ExternalException
            Me.TextBox1.Text = _
                "The Clipboard could not be accessed. Please try again."
        End Try

    End If

End Sub

.NET Framework
Available since 2.0
Return to top