Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding Row in DataGridView Very Slow

I have a DataGridView in a Winforms app that has about 1000 rows (unbound) and 50 columns. Hiding a column takes a full 2 seconds. When I want to hide about half the rows, this becomes a problem.

    private void ShowRows(string match)
    {
        this.SuspendLayout();
        foreach (DataGridViewRow row in uxMainList.Rows)
        {
            if (match == row.Cells["thisColumn"].Value.ToString()))
            { row.Visible = false; }
            else
            { row.Visible = true; }
        }
        this.ResumeLayout();
    }

I did some testing by adding by addingConsole.WriteLine(DateTime.Now)around the actions, androw.Visible = falseis definitely the slow bit. Am I missing something obvious, like setting IsReallySlow = false? Or do I have to go ahead and enable Virtual Mode and code up the necessary events?

like image 413
Ed Schwehm Avatar asked Mar 20 '10 13:03

Ed Schwehm


2 Answers

It looks to me like you should be using row filters instead.

Try using a DataView as your binding source and use DataView.RowFilter to hide rows or show rows of your choosing.

DataGridView myGridView = new DataGridView();
DataView myDataView = myTable.DefaultView;
myGridView.DataSource = myDataView; // DataView that allows row filtering

myDataView.RowFilter = string.Format("thisColumn <> '{0}'",match);  // this will hide all rows where "thisColumn" = match
like image 105
galford13x Avatar answered Sep 17 '22 05:09

galford13x


In most cases the property DataGridViewAutoSizeColumnMode makes a DGV slow. Your performance increases drastically when you change all columns to Mode DataGridViewAutoSizeColumnMode.None. Afterwards you can reset it in the same way to the previous state.

For Each col As DataGridViewColumn In myDGV.Columns
   col.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
Next

You will see that hiding some of 1000 columns now takes only 1-2 seconds. With other properties (SuspendLayout, Hiding the whole form etc.) I could not find any effect.

like image 28
bajuuu Avatar answered Sep 18 '22 05:09

bajuuu