I'am populating a datagridview from a datatable.
While populating the columns and rows, I also format it at the same time, this cause the datagridview to load very slowly, is there a work around for this problem?
As well as taking care of AutoSizeColumnsMode
, make sure that individual columns have their AutoSizeMode
property also set to something other than all cells.
I also found it necessary to use
SendMessage(dg.Handle, WM_SETREDRAW, false, 0); // before
// updates to datagridview here...
SendMessage(dg.Handle, WM_SETREDRAW, true, 0); // after
With this will be datagridview fast as java jtable :)
public static class ExtensionMethods
{
public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}
}
ExtensionMethods.DoubleBuffered(dataGridView1, true);
I was taking about 2-4 minutes to load 1-2k rows. I changed the auto-resize property and now it's down to seconds, probably 10-20. I ran this right before my row creation loop to ensure it got all the columns.
foreach (DataGridViewColumn c in thisGrid.Columns)
{
c.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
}
You could check the property of DataGridView - AutoSizeColumnsMode I found that if I change the mode from AllCells to DisplayedCells the performance is different. I hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With