dotnet / dotnet/wpf

WPF DataGrid HTML clipboard data faulty

Open
#10,476 17 comments 4 reactions 0 assignees View on GitHub
:construction: work in progress Bug Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.