Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataGridView.ClipboardCopyMode Property

Note: This property is new in the .NET Framework version 2.0.

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)

public DataGridViewClipboardCopyMode ClipboardCopyMode { get; set; }
/** @property */
public DataGridViewClipboardCopyMode get_ClipboardCopyMode ()

/** @property */
public void set_ClipboardCopyMode (DataGridViewClipboardCopyMode value)

public function get ClipboardCopyMode () : DataGridViewClipboardCopyMode

public function set ClipboardCopyMode (value : DataGridViewClipboardCopyMode)

Property Value

One of the DataGridViewClipboardCopyMode values. The default is EnableWithAutoHeaderText.

Exception typeCondition

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

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft