none
Hidding rows in the DataGridView is too slow

    Question

  • Hello all,

    Currently, I have a project that uses a dataGridView that contains some 10,000 rows of data. The user must have the ability to hide/unhide some or all of the rows. To accomplish this, I am setting the datagridView.dataGridViewRow.Visible property to true or false. This is done once for each row in the grid. This happens to work fine accept that it takes about 90 seconds to hide all of the rows. This is big trouble for me because my application may have several dataGridViews (and other views) sharing the same underlying data and the time cost must be paid for each one.

    My question is this...
    Is there a faster way to hide a large collection of rows?
    Tuesday, November 22, 2005 8:45 PM

Answers

  • Suspend and Resume layout will definitely be helpful. In addition, using VirtualMode vs. actually storing the data in the grid will help. With VirtualMode you would remove and add the rows instead of setting the visible property.

     

    In addition, you might get better perf if you add/remove the rows vs. using the visible property. The reason for this is that invisible rows will be enumerated each time when attempting to find what the next non-visible row is, so invisible might cause more problems than what you are trying to solve. I recommend doing some simple perf tests (e.g. writing out the time it takes to add/remove VS show/hide rows)

     

    -mark

    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

     

    Monday, December 05, 2005 11:41 PM

All replies

  • Did you utilize the SuspendLayout and ResumeLayout methods wrapped around your setting of the Visible properties?
    Monday, December 05, 2005 11:29 PM
  • Suspend and Resume layout will definitely be helpful. In addition, using VirtualMode vs. actually storing the data in the grid will help. With VirtualMode you would remove and add the rows instead of setting the visible property.

     

    In addition, you might get better perf if you add/remove the rows vs. using the visible property. The reason for this is that invisible rows will be enumerated each time when attempting to find what the next non-visible row is, so invisible might cause more problems than what you are trying to solve. I recommend doing some simple perf tests (e.g. writing out the time it takes to add/remove VS show/hide rows)

     

    -mark

    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"

     

    Monday, December 05, 2005 11:41 PM
  • Another side effect of "hiding" rows is that the alternate row coloring feature is pretty much mangled since it goes off of the actual rows existing, not whether they are visible.
    Tuesday, December 06, 2005 1:25 AM
  • plx!

    i wonder if you can provide some code example of removing rows in virtual mode in datagridview.

    I need to add/remove rows in my datagridview which is fired by a combobox selectedindexchanged event, I ve found i have to use Manager or somethin like that but I prefer to go trought virtual mode.

    Hope u can help

    Regards

    Joseph

    Thursday, January 04, 2007 9:23 PM
  • My question is this...
    Is there a faster way to hide a large collection of rows?

     

    >> I'm exactly asking the same thing ...To filter a DataGridView ... I collect row indexes that must be hidden in a table called Affich and then I make :

     

    For Each index As Integer In Affich
    MyDataGridView.Rows(index).Visible = False
    Next


    The problem is that loop is very very slow (120 seconds to hide 6000 rows in a 14000 row datagridview), and I really need to find a way to make it much faster ...  but I have no idea how to...

    Tuesday, May 29, 2007 8:13 PM
  • I have a huge performance issue when rendering DataGridView rows invisible: making ~400 rows invisible (out of ~400, actually...) takes 64 seconds!!!  Making them visible again, however, takes only 3 seconds (way too much, but still not absurd 64 seconds).

    It seems like the formatting/validating/parsing occurs over and over again and causing the performance hit.  As you can see below, I've tried various suggestions on this forum, but it did not improve the stats.

     

    Code Snippet

    CurrencyManager cm = (CurrencyManager)BindingContext[myDataGridView.DataSource];

    cm.SuspendBinding();

    myDataGridView.Visible = false;

    myDataGridView.SuspendLayout();

    for (int i = 0; i < myDataGridView.Rows.Count; i++)

    myDataGridView.Rows[i].Visible = someReadOnlyLogic();

    myDataGridView.ResumeLayout();

    myDataGridView.Visible = true;

    cm.ResumeBinding();

     

    I thought it was my custom formatting/validating/parsing which caused this code to slow down, so I removed the event handlers before the above code, but still - nothing.

    What am I missing?  Any leads I should follow?  Any help would be greatly appreciated.

     

    BTW, The datasource of the DataGridView object is List

    Thanks,

      Ury

     

    /* edit */
    I have considerably imporved the situation by adding the following code before and after changing the Row.Visible status:

     

    Code Snippet

    /* before */

    myDataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;

    myDataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;

     

    /* ... */

     

    /* after: restore the original mode */

    myDataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;

    myDataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;

     

     

    It still takes about 3 seconds to hide/unhide the 400 lines, which I think is absurd, but it's much better than a whole minute Smile

    If anybody has an idea how to achieve reasonable times, please let me know

     

    Wednesday, May 30, 2007 5:53 PM
  • Thanks for your help, but your additional code didn't help in my case, as AutoSizeMode property is set to None before changing te Row.Visible status. I definitely don't find anything to improve Datagridview Rows hiding ...
    Thursday, May 31, 2007 6:46 PM
  • If you still need it...

     

    Try filterring the data in the DataGridView using the attached dataView insted of setting specific visiblity for each row.

    Saturday, August 04, 2007 6:58 PM
  • I know this thread is old but I thought of posting my solution. I did not wan't to remove rows, I needed to hide them.
    I solved this problem by subclassing DataGridView and overriding OnRowStateChanged method.
    Before hiding rows set Suspended property to true and after that set to true.
    When Suspended property is set to false one row is needed to change it's visibility so grid is properly updated.

            private class DataGridViewEx: DataGridView
            {
                private bool m_Suspended = false;
                public bool Suspended
                {
                    get { return m_Suspended; }
                    set
                    {
                        if(m_Suspended != value) {
                            m_Suspended = value;
                            if(!m_Suspended && this.Rows.Count > 0) {
                                bool visible = this.Rows[0].Visible;
                                this.Rows[0].Visible = !visible;
                                this.Rows[0].Visible = visible;
                            }
                        }
                    }
                }
    
                protected override void OnRowStateChanged(int rowIndex, DataGridViewRowStateChangedEventArgs e)
                {
                    if(!m_Suspended) base.OnRowStateChanged(rowIndex, e);
                }
            }
    • Proposed as answer by Dog Ears Wednesday, May 20, 2009 11:24 AM
    Saturday, April 11, 2009 10:37 PM
  • Tuesday, August 31, 2010 1:30 PM