Language

DataGridView.GetClipboardContent メソッド

定義

Clipboardにコピーするために選択したセルの内容を表す書式設定された値を取得します。

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

返品

選択したセルの内容を表す DataObject 。

例外

ClipboardCopyMode は Disable に設定されます。

例

次のコード例は、選択した DataGridView コンテンツをプログラムでクリップボードに追加する方法を示しています。 この例は、「 方法: ユーザーが Windows フォーム DataGridView コントロールからクリップボードに複数のセルをコピーできるようにする」で使用できる、より大きな例の一部です。

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

注釈

このメソッドは、選択したセルによって定義された領域を表すデータを取得します。 この領域は、選択したすべてのセルを含む最小の四角形です。 この領域内で選択されている各セルの値は、 DataGridViewCell.GetClipboardContent メソッドを呼び出すことによって取得されます。 空白のプレースホルダー値は、この領域の選択されていないセルに使用されます。 このメソッドは、これらの値をクリップボードにコピーするための複数の形式を含む DataObject に結合します。 サポートされているクリップボード形式には、 DataFormats.Text、 DataFormats.UnicodeText、 DataFormats.Html、および DataFormats.CommaSeparatedValueが含まれます。

詳細については、Clipboard クラスを参照してください。

注意 (継承者)

カスタマイズされたクリップボード値を指定するには、このメソッドをオーバーライドします。 これは、たとえば、カスタム セル型からの値のコピーをサポートする場合に便利です。

適用対象

こちらもご覧ください