1

I'm copying a DataGrid to the clipboard so that it can be pasted to e.g. Excel while maintaining its format like this:

MyDataGrid.SelectAllCells();
MyDataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, MyDataGrid);

This works very nice. However, I need to add the string "MyDataGridTitle". If pasted to Excel, this should simply stand above the DataGrid.

I have tried various ways (e.g. use a DataObject) and tortured google, but to no success. I'd be thankful for a hint, tip or answer!

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
peter
  • 2,103
  • 7
  • 25
  • 51
  • You can't put multiple items in the clipboard. You can use the [Clipboard](https://msdn.microsoft.com/en-us/library/System.Windows.Clipboard(v=vs.110).aspx) class to copy data to the clipboard or read them. Perhaps you can read what DataGrid places there and modify it – Panagiotis Kanavos May 28 '15 at 14:15
  • I didn't do anything with the Clipboard yet but might it be useful to use `Clipboard.SetData` and provide an instance of `IDataObject` or `DataObject` as a parameter? You could use a complex object here that contains a header and the MyDataGrid contents. The only thing I don't know is how this object get's serialized and which serialization format is expected by Excel. https://msdn.microsoft.com/en-us/library/637ys738(v=vs.110).aspx – feO2x May 28 '15 at 14:51
  • feO2x, thanks, I've tried the DataObject, but that is not pasteable in Excel. – peter May 28 '15 at 15:32

1 Answers1

1

This is not a very elegant solution, but you can try by manipulating the html string generated by your DataGrid (indeed when you paste in Excel, the DataFormats.Html format is the one which is used).

Something like this:

dg.SelectAllCells();
dg.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dg);

string dataGridContent = (string)Clipboard.GetData(DataFormats.Html);
dataGridContent = dataGridContent.Replace("<TABLE>",
    String.Format("<TABLE><TR><TD colspan='{0}'>Your additional text<TD></TR>", dg.Columns.Count));

Clipboard.SetText(dataGridContent, TextDataFormat.Html);

Of course you can improve this code for example by using regular expressions instead of the Replace method.

Il Vic
  • 5,576
  • 4
  • 26
  • 37
  • Great! PS: when you are at my level, then this *is* a very elegant solution :o) – peter May 28 '15 at 16:21
  • One thing I notice is that the resulting string is cut off at the end: the last cells are empty when copied to excel, even though there are values in the datagrid. I guess it's because there are Information about STARTHTML and ENDHTML in the string 'dataGridContent' – peter May 28 '15 at 16:43
  • Ok, I'm adjusting EndHTML and EndFragment by the length of the replacing string and it's working as desired now. I'd now agree in that it's not a *very elegant* solution, but it's a solution nevertheless ;o) and I did not know that you could copy using html format. Thanks again. – peter May 28 '15 at 18:05