adding 10,000 rows into a DataGridView takes 10 seconds

I am adding 10,000 rows into a DataGridView with 50 columns, this takes 10 seconds - 6 seconds to add rows and 4 seconds to set initial values. :(

What is interesting - setting 2nd column values takes NO time

SuspendLayout does not help, there is no BeginUpdate method...

Any trick to improve

here is an example of my output:

now only adding rows 11/30/2006 3:56:05 AM
now setting 1st value 11/30/2006 3:56:11 AM
now setting 2ns value 11/30/2006 3:56:14 AM
done! 11/30/2006 3:56:14 AM

here is the code:

int c = 10000;

dataGridView1.SuspendLayout();
Debug.WriteLine("now only adding rows " + DateTime.Now);

for (int i = 0; i < c; i++)
{
dataGridView1.Rows.Add();
}

Debug.WriteLine("now setting 1st value " + DateTime.Now);
for (int i = 0; i < c; i++)
{
dataGridView1.RowsIdea.Cells[1].Value = "val 1 " + r.Next();
}

Debug.WriteLine("now setting 2ns value " + DateTime.Now);
for (int i = 0; i < c; i++)
{
dataGridView1.RowsIdea.Cells[3].Value = "val 2 " + r.Next();
}

dataGridView1.ResumeLayout();
Debug.WriteLine("done! " + DateTime.Now);




Answer this question

adding 10,000 rows into a DataGridView takes 10 seconds

  • junnie

    timvw wrote:
    Setting the ColumnHeadersHeightSize to DisableResizing... (and a couple of other properties that do resizing) will drastically speed up performance...

    Exactly same timing with 5 sizing properties set to false/none. What are there you mention as others

    Anyways, thank you anyways...

    int c = 10000;


    dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
    dataGridView1.AllowUserToResizeColumns = false;
    dataGridView1.AllowUserToResizeRows = false;

    dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;

    dataGridView1.SuspendLayout();
    Debug.WriteLine("now only adding rows " + DateTime.Now);

    for (int i = 0; i < c; i++)
    {
    dataGridView1.Rows.Add();
    dataGridView1.RowsIdea.Cells[1].Value = "val 1 " + r.Next();
    dataGridView1.RowsIdea.Cells[3].Value = "val 2 " + r.Next();
    }

    dataGridView1.ResumeLayout();
    Debug.WriteLine("done! " + DateTime.Now);



  • John K&amp;#228;ll&amp;#233;n

    Since you already have the answer, you'd might as well ignore everyone else and mark your thread.

     

    Adamus



  • Freddo

    i hope you are not trying to say this is the reason of the delay, right

    i am sorry but your reply has nothing to do with the topic of this thread, which is adding rows in a DataGridView

    specially for you i can split my question into two - "why adding 10,000 rows take 6 seconds" and "why setting 10,000 cell values take 4 seconds". can you somehow help me on this



  • lbamburg

    Don't run 3 - for loops. If all are going to iterate the exact amount, put them all in the same loop.

    10,000 loops < 30,000

               int c = 10000;

                dataGridView1.SuspendLayout();
                Debug.WriteLine("now only adding rows " + DateTime.Now);

                for (int i = 0; i < c; i++)
                {
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows.Cells[1].Value = "val 1 " + r.Next();
                    dataGridView1.Rows.Cells[3].Value = "val 2 " + r.Next();

                }

    Adamus



  • tarponboy11

    Have you tried putting the data in a DataTable first, then binding your grid to the datatable
  • Mike McGrath

    Setting the ColumnHeadersHeightSize to DisableResizing... (and a couple of other properties that do resizing) will drastically speed up performance...


  • progames25

    > Your poor programming practice and idiocracy is sure to amuse most of us, and steer the rest away

    Wow, you sound like you really believe in what you are saying. It is funny :)



  • c_shah

    You're using a gridview for 10,000 records with 50 columns and you don't understand why it's taking so long

    I'd love to see and use that application or form.



  • adding 10,000 rows into a DataGridView takes 10 seconds