共用方式為


DataGridViewClipboardCopyMode 列舉

定義

定義常數,指出內容是否會從 DataGridView 控制項複製到剪貼簿中。

public enum class DataGridViewClipboardCopyMode
public enum DataGridViewClipboardCopyMode
type DataGridViewClipboardCopyMode = 
Public Enum DataGridViewClipboardCopyMode
繼承
DataGridViewClipboardCopyMode

欄位

Disable 0

複製到剪貼簿的功能已停用。

EnableAlwaysIncludeHeaderText 3

選取儲存格的文字值可以複製到剪貼簿中。 而且,會針對包含選取儲存格的資料列與資料行包括其行首文字。

EnableWithAutoHeaderText 1

選取儲存格的文字值可以複製到剪貼簿中。 不過,只有當 SelectionMode 屬性設定為 RowHeaderSelectColumnHeaderSelect,而且至少選取一個行首時,才會針對包含選取儲存格的資料列或資料行包括其資料列或資料行行首文字。

EnableWithoutHeaderText 2

選取儲存格的文字值可以複製到剪貼簿中。 不過,不包括行首文字。

範例

下列程式碼範例示範如何在 控制項中 DataGridView 啟用複製。 如需完整範例,請參閱如何:讓使用者從 dataGridView 控制項Windows Forms將多個儲存格複製到剪貼簿。

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

備註

屬性會使用此 ClipboardCopyMode 列舉,指出使用者是否可以將選取儲存格的文字值複製到剪貼簿,以及是否包含資料列和資料行行首文字。

適用於

另請參閱