共用方式為


HOW TO:讓使用者從 Windows Form DataGridView 控制項將多個儲存格複製至剪貼簿

更新:2007 年 11 月

當您啟用儲存格複製時,會使 DataGridView 控制項中的資料更容易透過 Clipboard 讓其他應用程式存取。選取儲存格的值會轉換為字串,並以定位分隔文字值的形式加入至 [剪貼簿],以貼到應用程式 (例如,Notepad 和 Excel),或以 HTML 格式化資料表的形式貼入應用程式 (例如,Word)。

您可以將儲存格複製設定為只複製儲存格值,以包含 [剪貼簿] 資料中的資料列和資料行行首文字,或只在使用者選取整個資料列或資料行時才包含行首文字。

依據選取模式而定,使用者可以選取多個中斷連接的儲存格群組。當使用者複製儲存格至 [剪貼簿] 時,並不會複製沒有選取儲存格的資料列和資料行。所有其他資料列和資料行會變成複製至 [剪貼簿] 的資料表中的資料列和資料行。這些資料列或資料行中的未選取儲存格,會以空白的預留位置形式複製到 [剪貼簿] 中。

若要啟用儲存格複製

  • 設定 DataGridView.ClipboardCopyMode 屬性。

    Me.DataGridView1.ClipboardCopyMode = _
        DataGridViewClipboardCopyMode.EnableWithoutHeaderText
    
    this.DataGridView1.ClipboardCopyMode = 
        DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
    

範例

下列完整的程式碼範例示範如何複製儲存格到 [剪貼簿]。此範例包含使用 DataGridView.GetClipboardContent 方法將選取儲存格複製到 [剪貼簿] 的按鈕,並在文字方塊中顯示 [剪貼簿] 內容。

Imports System
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Private WithEvents DataGridView1 As New DataGridView()
    Private WithEvents CopyPasteButton As New Button()
    Private TextBox1 As New TextBox()

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub

    Public Sub New()

        Me.DataGridView1.AllowUserToAddRows = False
        Me.DataGridView1.Dock = DockStyle.Fill
        Me.Controls.Add(Me.DataGridView1)

        Me.CopyPasteButton.Text = "copy/paste selected cells"
        Me.CopyPasteButton.Dock = DockStyle.Top
        Me.Controls.Add(Me.CopyPasteButton)

        Me.TextBox1.Multiline = True
        Me.TextBox1.Height = 100
        Me.TextBox1.Dock = DockStyle.Bottom
        Me.Controls.Add(Me.TextBox1)

        Me.Text = "DataGridView Clipboard demo"

    End Sub

    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

End Class
using System;
using System.Windows.Forms;

public class Form1 : Form
{
    private DataGridView DataGridView1 = new DataGridView();
    private Button CopyPasteButton = new Button();
    private TextBox TextBox1 = new TextBox();

    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        this.DataGridView1.AllowUserToAddRows = false;
        this.DataGridView1.Dock = DockStyle.Fill;
        this.Controls.Add(this.DataGridView1);

        this.CopyPasteButton.Text = "copy/paste selected cells";
        this.CopyPasteButton.Dock = DockStyle.Top;
        this.CopyPasteButton.Click += new EventHandler(CopyPasteButton_Click);
        this.Controls.Add(this.CopyPasteButton);

        this.TextBox1.Multiline = true;
        this.TextBox1.Height = 100;
        this.TextBox1.Dock = DockStyle.Bottom;
        this.Controls.Add(this.TextBox1);

        this.Load += new EventHandler(Form1_Load);
        this.Text = "DataGridView Clipboard demo";
    }

    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.";
            }
        }
    }

}

編譯程式碼

這個程式碼需要:

  • N:System 和 N:System.Windows.Forms 組件的參考。

如需從 Visual Basic 或 Visual C# 的命令列建置這個範例的詳細資訊,請參閱從命令列建置 (Visual Basic)使用 csc.exe 建置命令列。您也可以透過將程式碼貼入新的專案,在 Visual Studio 中建置此範例。

請參閱

參考

DataGridView

ClipboardCopyMode

GetClipboardContent

其他資源

選取範圍和剪貼簿與 Windows Form DataGridView 控制項搭配使用