Expand Minimize

DataGridView.ClipboardCopyMode Property

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.

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

[BrowsableAttribute(true)]
public DataGridViewClipboardCopyMode ClipboardCopyMode { get; set; }

ExceptionCondition
InvalidEnumArgumentException

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

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.

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.";
        }
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft