Copy DataGridView contents to clipboard

2019-04-20 13:35发布

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

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

But this just pastes nothing.

Any suggestions?

2条回答
The star\"
2楼-- · 2019-04-20 13:39

Have you added this line?

myDataGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;

Take a look at this MSDN article for a working sample.

查看更多
Lonely孤独者°
3楼-- · 2019-04-20 13:55

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

查看更多
登录 后发表回答