This issue has been moved from a ticket on Developer Community.
[severity:It's more difficult to complete my work]
I have a WPF DataGrid bound to a List of structs. If I use Ctrl+A to select all the rows, sometimes it's instant and highlights all the rows regardless if there are hundreds of thousands, and sometimes (with exactly the same data) it can take minutes. In my main application I can execute the same program again and again with the same data but the Ctrl-A performance is hugely erratic. I've boiled this down to some simple code below which is more consistent - it takes about 25 seconds to select 5000 rows (about 200 per second). Seemingly random changes to the struct can make the problem go away and the selection then becomes instant. I'm targeting .NET 4.6.2 on a Ryzen 3950X with 32GB, no problems elsewhere.
Example code below with comments. Also see the attached project.
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<!--I've kept it simple -->
<Grid>
<DataGrid x:Name="myDataGrid" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace WpfApp1
{
//The problem: when the DataGrid is displayed, hitting CTRL+A (or using the DataGrid
//corner button to Select All rows) is very slow, taking about 1 second per 200 rows.
//In the example below with 5000 rows, it takes 25 seconds on my machine. In my
//larger project the issue is intermittent, sometimes working fine, sometimes not.
//For this example I've made the simplest version possible whilst still exhibiting
//the behaviour.
//
//Interestingly, if you change "struct" to "class" it seems to work and the
//Ctrl+A is instant, at least in the version here. In my main project the use of
//class or ObservableCollection makes no difference - it's still erratic, but
//here it seems to work.
//
//But it should work fine with structs shouldn't it? With structs there seem to be
//no issues other than the Ctrl-A, and scrolling is fine.
//
//Also interesting is that if you leave it as struct and move the "myChar" to the
//end of the struct rather than the beginning, that seems to work too, at least
//on my machine. Bizarre behaviour.
//
//More odd behaviour is that when all rows are selected (and highlighted in blue),
//scrolling down seems to show that not all are highlighted in blue.
public struct myStruct
{
public char myChar { get; set; }
public int myInt { get; set; }
public string myString1 { get; set; }
public string myString2 { get; set; }
public string myString3 { get; set; }
}
public partial class MainWindow : Window
{
List<myStruct> myList = new List<myStruct>();
public MainWindow()
{
InitializeComponent();
//Using a seed here to ensure all the randomised strings are the
//same from one execution to the next
Random random = new Random(12345678);
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//Generate the List. 5000 structs in this example.
for (int i = 0; i < 5000; i++)
{
myStruct s = new myStruct();
s.myChar = '?';
s.myInt = i;
//The below populates each string with 20 random characters
s.myString1 = new string(Enumerable.Repeat(chars, 20). Select(r => r[random. Next(r.Length)]). ToArray());
s.myString2 = new string(Enumerable.Repeat(chars, 20). Select(r => r[random. Next(r.Length)]). ToArray());
s.myString3 = new string(Enumerable.Repeat(chars, 20). Select(r => r[random. Next(r.Length)]). ToArray());
myList.Add(s);
}
//Set the DataGrid ItemsSource to the list
myDataGrid.ItemsSource = myList;
}
}
}
Original Comments
Feedback Bot on 8/29/2021, 08:10 PM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
Original Solutions
(no solutions)
This issue has been moved from a ticket on Developer Community.
[severity:It's more difficult to complete my work]
I have a WPF DataGrid bound to a List of structs. If I use Ctrl+A to select all the rows, sometimes it's instant and highlights all the rows regardless if there are hundreds of thousands, and sometimes (with exactly the same data) it can take minutes. In my main application I can execute the same program again and again with the same data but the Ctrl-A performance is hugely erratic. I've boiled this down to some simple code below which is more consistent - it takes about 25 seconds to select 5000 rows (about 200 per second). Seemingly random changes to the struct can make the problem go away and the selection then becomes instant. I'm targeting .NET 4.6.2 on a Ryzen 3950X with 32GB, no problems elsewhere.
Example code below with comments. Also see the attached project.
Original Comments
Feedback Bot on 8/29/2021, 08:10 PM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
Original Solutions
(no solutions)