WPF DataGrid HTML clipboard data faulty

Open Beginner friendly
#10,476 17 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
desktop

Research direction

Start in Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridClipboardHelper.cs, especially lines 86-102, and reproduce copying selected DataGrid content before pasting into Excel. Verify the HTML clipboard offsets against the generated content; done means Excel and other applications accept the copied HTML without requiring a workaround.

Written by the indexing model from the issue text.

Description

:construction: work in progress Bug Investigate
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.
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 6h
Merged PRs (30d)
75

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/wpf

All issues in dotnet/wpf

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.