DataGridView.GetClipboardContent 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索表示选定单元格内容的格式化值,以便将其复制到 Clipboard。
public:
virtual System::Windows::Forms::DataObject ^ GetClipboardContent();
public virtual System.Windows.Forms.DataObject GetClipboardContent ();
public virtual System.Windows.Forms.DataObject? GetClipboardContent ();
abstract member GetClipboardContent : unit -> System.Windows.Forms.DataObject
override this.GetClipboardContent : unit -> System.Windows.Forms.DataObject
Public Overridable Function GetClipboardContent () As DataObject
返回
一个 DataObject,表示选定单元格的内容。
例外
示例
下面的代码示例演示如何以编程方式将所选 DataGridView 内容添加到剪贴板。 本示例是如何:使用户从 Windows 窗体 dataGridView 控件将多个单元格复制到剪贴板中提供的更大示例的一部分。
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
注解
此方法检索表示所选单元格定义的区域的数据。 此区域是包含所有选定单元格的最小矩形。 通过调用 DataGridViewCell.GetClipboardContent 方法检索此区域中每个选定单元格的值。 空白占位符值用于此区域中未选择的单元格。 此方法将这些值合并为一个 , DataObject 其中包含用于复制到剪贴板的多种格式。 支持的剪贴板格式包括 DataFormats.Text、 DataFormats.UnicodeText、 DataFormats.Html和 DataFormats.CommaSeparatedValue。
有关更多信息,请参见 Clipboard 类。
继承者说明
重写此方法以提供自定义的剪贴板值。 例如,这对于支持从自定义单元格类型复制值非常有用。