DataGridView.ClipboardCopyMode DataGridView.ClipboardCopyMode DataGridView.ClipboardCopyMode DataGridView.ClipboardCopyMode Property

定义

获取或设置一个值,该值指示用户是否可以将单元格的文本值复制到 Clipboard,以及是否包括行标题和列标题文本。Gets or sets a value that indicates whether users can copy cell text values to the Clipboard and whether row and column header text is included.

public:
 property System::Windows::Forms::DataGridViewClipboardCopyMode ClipboardCopyMode { System::Windows::Forms::DataGridViewClipboardCopyMode get(); void set(System::Windows::Forms::DataGridViewClipboardCopyMode value); };
[System.ComponentModel.Browsable(true)]
public System.Windows.Forms.DataGridViewClipboardCopyMode ClipboardCopyMode { get; set; }
member this.ClipboardCopyMode : System.Windows.Forms.DataGridViewClipboardCopyMode with get, set
Public Property ClipboardCopyMode As DataGridViewClipboardCopyMode

属性值

异常

当设置此属性时,指定的值不是有效的 DataGridViewClipboardCopyMode 值。The specified value when setting this property is not a valid DataGridViewClipboardCopyMode value.

示例

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

注解

DataGridView控件将每个所选单元格的文本表示形式复制到剪贴板。The DataGridView control copies the text representation of each selected cell to the Clipboard. 此值是转换为字符串的单元值, 对于图像单元, 则为Description属性的值。This value is the cell value converted to a string or, for image cells, the value of the Description property.

Text值以UnicodeText 、、和CommaSeparatedValue格式复制到剪贴板。 HtmlValues are copied to the Clipboard in Text, UnicodeText, Html, and CommaSeparatedValue formats. 这些格式适用于将内容粘贴到记事本、Microsoft Excel 和Microsoft WordMicrosoft Word之类的应用程序中。These formats are useful for pasting content into applications such as Notepad, Microsoft Excel, and Microsoft WordMicrosoft Word.

DataGridView控件复制包含所选所有单元格的最小矩形中包含的单元。The DataGridView control copies the cells contained in the smallest rectangle that includes all selected cells. 不包含任何选定单元格的行和列不会在复制的数据中表示。Rows and columns that do not contain any selected cells are not represented in the copied data. 其余行和列中未选定的单元格由空白占位符表示。Any unselected cells in the remaining rows and columns are represented by blank placeholders. 根据复制模式和选择模式, 还可以复制复制的行和列的标头值。Depending on the copy mode and the selection mode, header values for the copied rows and columns may be copied, as well.

当用户复制内容时, DataGridView控件DataObject将添加到剪贴板。When users copy content, the DataGridView control adds a DataObject to the Clipboard. 将从GetClipboardContent方法中检索此数据对象。This data object is retrieved from the GetClipboardContent method. 如果要以编程方式将数据对象添加到剪贴板, 则可以调用此方法。You can call this method when you want to programmatically add the data object to the Clipboard.

GetClipboardContent方法通过DataGridViewCell.GetClipboardContent调用方法来检索各个单元格的值。The GetClipboardContent method retrieves values for individual cells by calling the DataGridViewCell.GetClipboardContent method. 您可以在派生类中重写这些方法中的一个或两个, 以自定义复制单元格的布局或支持其他数据格式。You can override either or both of these methods in derived classes to customize the layout of copied cells or to support additional data formats.

有关剪贴板操作和数据格式的详细信息, 请参阅Clipboard类。For more information about Clipboard operations and data formats, see the Clipboard class.

适用于

另请参阅