DataGridViewClipboardCopyMode 枚举

定义

定义常数,指示是否将内容从 DataGridView 控件复制到剪贴板。Defines constants that indicate whether content is copied from a DataGridView control to the Clipboard.

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

字段

Disable 0

禁用“复制到剪贴板”。Copying to the Clipboard is disabled.

EnableAlwaysIncludeHeaderText 3

可以将所选单元格的文本值复制到剪贴板。The text values of selected cells can be copied to the Clipboard. 为所选单元格所在的行和列包含标题文本。Header text is included for rows and columns that contain selected cells.

EnableWithAutoHeaderText 1

可以将所选单元格的文本值复制到剪贴板。The text values of selected cells can be copied to the Clipboard. 仅当 SelectionMode 属性设置为 RowHeaderSelectColumnHeaderSelect,并且至少选择了一个标题时,才为所选单元格所在的行或列包含行或列标题文本。Row or column header text is included for rows or columns that contain selected cells only when the SelectionMode property is set to RowHeaderSelect or ColumnHeaderSelect and at least one header is selected.

EnableWithoutHeaderText 2

可以将所选单元格的文本值复制到剪贴板。The text values of selected cells can be copied to the Clipboard. 不包含标题文本。Header text is not included.

示例

下面的代码示例演示如何在DataGridView控件中启用复制。The following code example demonstrates how to enable copying in the DataGridView control. 有关完整的示例,请参阅如何:使用户能够将多个单元格从 Windows 窗体 DataGridView 控件复制到剪贴板。For the complete example, see 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

注解

此枚举由ClipboardCopyMode属性用来指示用户是否可以将所选单元格的文本值复制到剪贴板, 以及是否包括行标题和列标题文本。This enumeration is used by the ClipboardCopyMode property to indicate whether users can copy the text values of selected cells to the Clipboard and whether row and column header text is included.

适用于

另请参阅