#10476·wpf

WPF DataGrid HTML clipboard data faulty

Author: kniessenCreated Feb 19, 2025Updated Sep 11, 2026
LabelsBugInvestigate:construction: work in progress

Description

The HTML fragment DataGrid puts into the clipboard is not accepted by other applications.

It appears that the fragment's offsets (StartHTML/EndHTML/StartFragment/EndFragment) do not match the content.

Reproduction Steps

Select all data in a DataGrid, copy and then paste to Excel.

Expected behavior

Excel (or other application) correctly accepts HTML data from DatGrid in clipboard.

Actual behavior

Application cannot interpret and paste HTML data copied from DataGrid but display error.

Regression?

Error was introduced with #8519.

Known Workarounds

  1. Derive DataGrid subclass and override OnExecutedCopy:
        protected override void OnExecutedCopy(ExecutedRoutedEventArgs args)
        {
            base.OnExecutedCopy(args);

            String htmlData = Clipboard.GetData(DataFormats.Html) as String;
            htmlData = htmlData.Replace("\r\n\r\n", "\r\n");
            Clipboard.SetData(DataFormats.Html, htmlData);
        }

2. Manually select csv or other format when pasting data copied from DataGrid in other applications.

### Impact

High impact at my company's backoffice which uses copy/paste to Excel intensely. On a more general scale, probably low.

### Configuration

.Net 9 Win11

### Other information

The problem with the wrong offsets is a result of double CR/LFs between the header items in Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridClipboardHelper.cs (lines 86-102) - remove those and everything works as before.