C# 将 DataGridView 内容复制到剪贴板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1108872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:26:35  来源:igfitidea点击:

Copy DataGridView contents to clipboard

c#datagridviewcopyclipboard

提问by Karl

I want to copy the contents of a DataGridView and paste it in Excel. I tried:

我想复制 DataGridView 的内容并将其粘贴到 Excel 中。我试过:

myDataGrid.SelectAll();
DataObject dataObj = myDataGrid.GetClipboardContent();
Clipboard.SetDataObject(dataObj, true)

But this just pastes nothing.

但这只是粘贴任何内容。

Any suggestions?

有什么建议?

采纳答案by Magnus Johansson

Have you added this line?

你添加了这一行吗?

myDataGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;

Take a look at this MSDN articlefor a working sample.

查看此 MSDN 文章以获取工作示例。

回答by faysalmirmd

If you use Microsoft Visual Studio You can do it in Design File. Your Gridview ->right Click->Propeties->ClipBoard Copy Mode->EnableWithoutHeaderText

如果您使用 Microsoft Visual Studio,您可以在设计文件中进行。您的 Gridview -> 右键单击​​-> 属性-> 剪贴板复制模式-> EnableWithoutHeaderText