如何:使用户能够将多个单元格从 Windows 窗体 DataGridView 控件复制到剪贴板
更新:2007 年 11 月
启用单元格复制时,其他应用程序将能够很容易地通过 Clipboard 访问 DataGridView 控件中的数据。选择的单元格的值会转换为字符串,并作为以制表符分隔的文本值添加到剪贴板中,以便粘贴到类似记事本和 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 中生成此示例。 如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例
如何:使用 Visual Studio 编译和运行完整的 Windows 窗体代码示例