Skip to content

WPF DataGrid HTML clipboard data faulty #10476

@kniessen

Description

@kniessen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    🚧 work in progressBugProduct bug (most likely)InvestigateRequires further investigation by the WPF team.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions