Redigera

Dela via


DataGridView.ClipboardCopyMode Property

Definition

Gets or sets a value that indicates whether users can copy cell text values to the Clipboard and whether row and column header text is included.

public:
 property System::Windows::Forms::DataGridViewClipboardCopyMode ClipboardCopyMode { System::Windows::Forms::DataGridViewClipboardCopyMode get(); void set(System::Windows::Forms::DataGridViewClipboardCopyMode value); };
[System.ComponentModel.Browsable(true)]
public System.Windows.Forms.DataGridViewClipboardCopyMode ClipboardCopyMode { get; set; }
[<System.ComponentModel.Browsable(true)>]
member this.ClipboardCopyMode : System.Windows.Forms.DataGridViewClipboardCopyMode with get, set
Public Property ClipboardCopyMode As DataGridViewClipboardCopyMode

Property Value

One of the DataGridViewClipboardCopyMode values. The default is EnableWithAutoHeaderText.

Attributes

Exceptions

The specified value when setting this property is not a valid DataGridViewClipboardCopyMode value.

Examples

The following code example demonstrates how to enable copying in the DataGridView control. 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 void Form1_Load(object sender, System.EventArgs e)
{
    // Initialize the DataGridView control.
    this.DataGridView1.ColumnCount = 5;
    this.DataGridView1.Rows.Add(new string[] { "A", "B", "C", "D", "E" });
    this.DataGridView1.Rows.Add(new string[] { "F", "G", "H", "I", "J" });
    this.DataGridView1.Rows.Add(new string[] { "K", "L", "M", "N", "O" });
    this.DataGridView1.Rows.Add(new string[] { "P", "Q", "R", "S", "T" });
    this.DataGridView1.Rows.Add(new string[] { "U", "V", "W", "X", "Y" });
    this.DataGridView1.AutoResizeColumns();
    this.DataGridView1.ClipboardCopyMode = 
        DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
}

private void CopyPasteButton_Click(object sender, System.EventArgs e)
{
    if (this.DataGridView1
        .GetCellCount(DataGridViewElementStates.Selected) > 0)
    {
        try
        {
            // Add the selection to the clipboard.
            Clipboard.SetDataObject(
                this.DataGridView1.GetClipboardContent());
            
            // Replace the text box contents with the clipboard text.
            this.TextBox1.Text = Clipboard.GetText();
        }
        catch (System.Runtime.InteropServices.ExternalException)
        {
            this.TextBox1.Text = 
                "The Clipboard could not be accessed. Please try again.";
        }
    }
}
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.AutoResizeColumns()
    Me.DataGridView1.ClipboardCopyMode = _
        DataGridViewClipboardCopyMode.EnableWithoutHeaderText

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

        Try

            ' Add the selection to the clipboard.
            Clipboard.SetDataObject( _
                Me.DataGridView1.GetClipboardContent())

            ' 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

Remarks

The DataGridView control copies the text representation of each selected cell to the Clipboard. This value is the cell value converted to a string or, for image cells, the value of the Description property.

Values are copied to the Clipboard in Text, UnicodeText, Html, and CommaSeparatedValue formats. These formats are useful for pasting content into applications such as Notepad, Microsoft Excel, and Microsoft Word.

The DataGridView control copies the cells contained in the smallest rectangle that includes all selected cells. Rows and columns that do not contain any selected cells are not represented in the copied data. Any unselected cells in the remaining rows and columns are represented by blank placeholders. Depending on the copy mode and the selection mode, header values for the copied rows and columns may be copied, as well.

When users copy content, the DataGridView control adds a DataObject to the Clipboard. This data object is retrieved from the GetClipboardContent method. You can call this method when you want to programmatically add the data object to the Clipboard.

The GetClipboardContent method retrieves values for individual cells by calling the DataGridViewCell.GetClipboardContent method. You can override either or both of these methods in derived classes to customize the layout of copied cells or to support additional data formats.

For more information about Clipboard operations and data formats, see the Clipboard class.

Applies to

See also