Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# very slow populating the datagridview

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?

like image 263
user1030916 Avatar asked Nov 05 '11 08:11

user1030916


4 Answers

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
like image 174
merry-v Avatar answered Oct 26 '22 07:10

merry-v


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);
like image 37
mato Avatar answered Oct 26 '22 08:10

mato


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;
}
like image 7
asdf30 Avatar answered Oct 26 '22 08:10

asdf30


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.

like image 5
Chanev Avatar answered Oct 26 '22 08:10

Chanev