DataGrid copy doesn't respect StringFormat

Feb 11, 2010 at 8:26 PM

Hi -

If I perform a copy of a DataGrid row to the clipboard, and the DataGridTextBoxColumn Binding (or ClipboardContentBinding) has a StringFormat in it, it is ignored in the data placed on the clipboard.  Converters in the binding, however, are respected, so I can work around the problem by using a StringFormatConverter, much as I would do before 3.5 SP1.  Obviously, though, that's not ideal.

Is this by design?  It doesn't seem like an intuitive behavior.



Feb 6, 2011 at 3:09 AM

We are having the same issue.  Another option is to use the following event handler CopyingCellClipboardContent


private void DataGridTextColumn_CopyingCellClipboardContent(object sender, DataGridCellClipboardEventArgs e)


  if (null != e.Column.ClipboardContentBinding && !string.IsNullOrEmpty(e.Column.ClipboardContentBinding.StringFormat))


      e.Content = string.Format(e.Column.ClipboardContentBinding.StringFormat, e.Content);



Also, note that for the below to work, the StringFormat for the binding has to be in the following format:

{\{0:xx\}}, where xx is the format string, like {\{0:C\}}.  You can’t just use StringFormat=C like you can with the grid, or else the code behind on the string.Format doesn’t work.  The format {}{0:C} might also work, although I did not check that.

We are looking at putting this into an attached behavior rather than having to add the handler to each column of the datagrid.  Of course, I'd much rather see the datagrid handle this for us, and if you wanted some control, you could specify, at the datagrid level, whether you wanted the copytoclipboard functionality to use the stringformat or not.  If anyone knows any different, please let me know.  We are using the June 2009 datagrid right now.