DataGridView.GetClipboardContent Método

Definición

Recupera los valores con formato que representan el contenido de las celdas seleccionadas para copiarlos al objeto Clipboard.Retrieves the formatted values that represent the contents of the selected cells for copying to the Clipboard.

public:
 virtual System::Windows::Forms::DataObject ^ GetClipboardContent();
public virtual System.Windows.Forms.DataObject GetClipboardContent ();
abstract member GetClipboardContent : unit -> System.Windows.Forms.DataObject
override this.GetClipboardContent : unit -> System.Windows.Forms.DataObject
Public Overridable Function GetClipboardContent () As DataObject

Devoluciones

Objeto DataObject que representa el contenido de las celdas seleccionadas.A DataObject that represents the contents of the selected cells.

Excepciones

El valor deClipboardCopyMode está establecido en Disable.ClipboardCopyMode is set to Disable.

Ejemplos

En el ejemplo de código siguiente se muestra cómo agregar mediante programación el contenido de DataGridView seleccionado al portapapeles.The following code example demonstrates how to programmatically add selected DataGridView content to the Clipboard. Este ejemplo forma parte de un ejemplo más grande disponible en Cómo: permitir que los usuarios copien varias celdas en el Portapapeles desde el control DataGridView Windows Forms.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

Comentarios

Este método recupera datos que representan la región definida por las celdas seleccionadas.This method retrieves data that represents the region defined by the selected cells. Esta región es el rectángulo más pequeño que incluye todas las celdas seleccionadas.This region is the smallest rectangle that includes all of the selected cells. El valor de cada celda seleccionada en esta región se recupera llamando al método DataGridViewCell.GetClipboardContent.The value for each selected cell in this region is retrieved by calling the DataGridViewCell.GetClipboardContent method. Se usan valores de marcador de posición en blanco para las celdas no seleccionadas en esta región.Blank placeholder values are used for unselected cells in this region. Este método combina estos valores en una DataObject que contiene varios formatos para copiar en el portapapeles.This method combines these values into a DataObject containing several formats for copying to the clipboard. Entre los formatos de Portapapeles admitidos se incluyen DataFormats.Text, DataFormats.UnicodeText, DataFormats.Htmly DataFormats.CommaSeparatedValue.The supported clipboard formats include DataFormats.Text, DataFormats.UnicodeText, DataFormats.Html, and DataFormats.CommaSeparatedValue.

Para obtener más información, vea la clase Clipboard.For more information, see the Clipboard class.

Notas a los desarrolladores de herederos

Invalide este método para proporcionar valores personalizados del portapapeles.Override this method to provide customized clipboard values. Esto resulta útil, por ejemplo, para admitir la copia de valores de tipos de celda personalizados.This is useful, for example, to support copying values from custom cell types.

Se aplica a

Consulte también: