Copy DataGridView contents to clipboard

2019-04-20 13:43发布

问题:

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?

回答1:

Have you added this line?

myDataGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;

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



回答2:

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