C# – Copy DataGridView contents to clipboard


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

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

But this just pastes nothing.

Any suggestions?

Best Solution

Have you added this line?

myDataGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;

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