Export (0) Print
Expand All
Expand Minimize

DataGridView.GetClipboardContent Method

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 virtual DataObject GetClipboardContent()

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 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, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft