DataGridView.ClipboardCopyMode 屬性

定義

取得或設定值,指出使用者是否可以將儲存格文字值複製到 Clipboard,以及是否會包含資料列和資料行標頭文字。

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; }
[<System.ComponentModel.Browsable(true)>]
member this.ClipboardCopyMode : System.Windows.Forms.DataGridViewClipboardCopyMode with get, set
Public Property ClipboardCopyMode As DataGridViewClipboardCopyMode

屬性值

其中一個 DataGridViewClipboardCopyMode 值。 預設值為 EnableWithAutoHeaderText

屬性

例外狀況

設定這個屬性時所指定的值不是有效的 DataGridViewClipboardCopyMode 值。

範例

下列程式碼範例示範如何在 控制項中 DataGridView 啟用複製。 此範例是 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 會將每個選取儲存格的文字表示複製到剪貼簿。 這個值是轉換成字串的儲存格值,或者,如果是影像儲存格,則為 屬性的值 Description

值會以 、 UnicodeTextHtmlCommaSeparatedValue 格式複製到剪貼簿 Text 。 這些格式適用于將內容貼入記事本、Microsoft Excel 和 Microsoft Word等應用程式。

控制項 DataGridView 會複製包含所有選取儲存格的最小矩形中包含的儲存格。 不包含任何選取儲存格的資料列和資料行不會在複製的資料中表示。 其餘列和資料行中的任何未選取儲存格都以空白預留位置表示。 視複製模式和選取模式而定,也可以複製所複製資料列和資料行的標頭值。

當使用者複製內容時, DataGridView 控制項會將 新增 DataObject 至剪貼簿。 這個資料物件是從 方法擷 GetClipboardContent 取。 當您想要以程式設計方式將資料物件新增至剪貼簿時,可以呼叫這個方法。

方法 GetClipboardContent 會藉由呼叫 DataGridViewCell.GetClipboardContent 方法來擷取個別儲存格的值。 您可以在衍生類別中覆寫或兩種方法,以自訂複製儲存格的配置,或支援其他資料格式。

如需剪貼簿作業和資料格式的詳細資訊,請參閱 類別 Clipboard

適用於

另請參閱