I know there are several questions in SO about slow DataGridViews, but I tried really everything mentioned there and most are about more complex databinding.
what I did before:
- I tested DataTable as Datasource
- unbound Datagridview, populated in a loop
- tested many setting with autoColumnResizing etc. all tests are too slow.
This example is only a datgridview in a main form, plain stock from Designer's Toolbox, edit off.
Even if it's only 100 lines it feels very laggy when scrolling and redrawing and it's maxing out one core.
I did a VS profiler session when scrolling in the datagridview:
I'd like to dig deeper and understand what exactly makes this control so slow and what can be done to accelerate it or what alternatives I can use to get a fast table which is able to be sorted by clicking the column headers.
public partial class Form1 : Form
{
List<Record> myData;
public Form1()
{
InitializeComponent();
myData = getListofRecord();
dataGridView1.DataSource = myData;
}
private static List<Record> getListofRecord()
{
var myTable = new List<Record>();
for (int i = 0; i < 500; i++)
{
myTable.Add(new Record() { a = 121+i, b = 123.2434F, c = 2342, d = 312+i*2, e = 123343, f = 12323 });
}
return myTable;
}
}
public class Record
{
public Single a { get; set; }
public Single b { get; set; }
public Single c { get; set; }
public Single d { get; set; }
public Single e { get; set; }
public Single f { get; set; }
}
Edit:
2 days later I get a different result when rerunning the test app on the same machine.
It's quite as fast as I would expect and the profiling may lead to the reason?
in the hotpath stack the CreateSemiCompatibleDIB
is missing.
Edit2:
whatever the true reason behind the 1st difference is, turning on double buffering by reflection did the most obvious and roughly 20x faster effect. I have seen these suggestion in other DGV questions but was only expecting to get a slight visual improvement, not a complete new experience.
the profiling hotpath now shows a completely different call stack.
Edit3
to save my profiling work in this question and not being deleted as duplicate I would like to get some objective numbers in milliseconds for a redraw of the DGV. Where do I need to place a stopwatch to get these?