DataGridView.GetClipboardContent Methode

Definitie

Hiermee worden de opgemaakte waarden opgehaald die de inhoud van de geselecteerde cellen vertegenwoordigen voor het kopiëren naar de Clipboardcel.

public:
 virtual System::Windows::Forms::DataObject ^ GetClipboardContent();
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

Retouren

Een DataObject die de inhoud van de geselecteerde cellen vertegenwoordigt.

Uitzonderingen

Voorbeelden

In het volgende codevoorbeeld ziet u hoe u programmatisch geselecteerde DataGridView inhoud toevoegt aan het Klembord. Dit voorbeeld maakt deel uit van een groter voorbeeld dat beschikbaar is in How to: Enable Users to Copy Multiple Cells to the Klemboard 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

Opmerkingen

Met deze methode worden gegevens opgehaald die het gebied vertegenwoordigen dat is gedefinieerd door de geselecteerde cellen. Dit gebied is de kleinste rechthoek met alle geselecteerde cellen. De waarde voor elke geselecteerde cel in deze regio wordt opgehaald door de DataGridViewCell.GetClipboardContent methode aan te roepen. Lege tijdelijke aanduidingen worden gebruikt voor niet-geselecteerde cellen in dit gebied. Deze methode combineert deze waarden in een DataObject indeling met verschillende indelingen voor het kopiëren naar het klembord. De ondersteunde klembordindelingen zijn onder andere DataFormats.Text, DataFormats.UnicodeText, DataFormats.Htmlen DataFormats.CommaSeparatedValue.

Zie de Clipboard klas voor meer informatie.

Notities voor overnemers

Overschrijf deze methode om aangepaste klembordwaarden op te geven. Dit is bijvoorbeeld handig om het kopiëren van waarden uit aangepaste celtypen te ondersteunen.

Van toepassing op

Zie ook