ehh
ehh

Reputation: 3480

Hiding multiple columns programmatically in DataGridView is very slow

I have a DatagridView with about 300 columns and 80 rows. Each column can be of 3 different types.

There are 3 check boxes which are responsible to show/hide the columns, each check box for each column type.

    private void HideColumns(DataGridView datagridview)
    {
        if (datagridview.DataSource == null) return;

        var watch = Stopwatch.StartNew();

        // Added this code further the comment
        Control c = datagridview;
        while (c != this)
        {
            c.SuspendLayout();
            c = c.Parent;
        }
        this.SuspendLayout();       

        CurrencyManager currencyManager = null;
        try
        {
            RemoveHandler(datagridview); // remove all the handlers to the datagridivew for performance issue
            currencyManager = (CurrencyManager)BindingContext[datagridview.DataSource];
            currencyManager.SuspendBinding();
            //datagridview.Visible = false;

            for (int i = 0; i < datagridview.Columns.Count; i++)
            {
                var column = datagridview.Columns[i];

                var itemType = datagridview.Rows[(int)OrdersAndComponentsRows.ItemType].Cells[column.Index].Value.ToString();
                if (itemType == Glossary.IndirectCOType )
                    column.Visible = IndirectCOCheckBox.Checked;
                else if (itemType == Glossary.NotAllocatedType )
                    column.Visible = NotAllocatedCheckBox.Checked;
                else 
                    column.Visible = DirectCOCheckBox.Checked;
            }
        }
        finally
        {
            //datagridview.Visible = true;
            if (currencyManager != null)
                currencyManager.ResumeBinding();
            AddHandler(datagridview);

            // Added this code further the comment
            c = datagridview;
            while (c != this)
            {
                c.ResumeLayout();
                c = c.Parent;
            }
            this.ResumeLayout();
        }

    // 3 check boxes are subscribed to this event
    private void DisplayColumn_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            Cursor = Cursors.WaitCursor;
            HideColumns(ShipCoverageDGV);
        }
        finally
        {
            Cursor = Cursors.Default;
        }
    }

The problem is when I am unchecking one of the checkboxes, it takes about 50 seconds to hide the columns. The weird thing is that when checking the checkbox, it takes around 5 seconds to show the hidden columns.

Is there any operation that can be done in order to hide columns faster?

Upvotes: 0

Views: 833

Answers (2)

ehh
ehh

Reputation: 3480

Setting AutoSizeRowMode to DisplayedHeaders solved the problem. It takes about 1 second. Before setting the value, this property was set to AllCells

datagridview.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedHeaders;

Note: Setting the property to None decrease the time to about 12 seconds.

The entire method including the fix:

    private void HideColumns(DataGridView datagridview)
    {
        try
        {
            datagridview.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedHeaders;

            for (int i = 0; i < datagridview.Columns.Count; i++)
            {
                var column = datagridview.Columns[i];

                var itemType = datagridview.Rows[(int)OrdersAndComponentsRows.ItemType].Cells[column.Index].Value.ToString();
                if (itemType == Glossary.IndirectCOType)
                    column.Visible = IndirectCOCheckBox.Checked;
                else if (itemType == Glossary.NotAllocatedType)
                    column.Visible = NotAllocatedCheckBox.Checked;
                else
                    column.Visible = DirectCOCheckBox.Checked;
            }
        }
        finally
        {
            datagridview.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        }
    }

Upvotes: 3

ErGaurav
ErGaurav

Reputation: 81

I think you need to figure out which part of the code is taking time.There are lots of things occurring in your hide columns method. Can you note down the time at the more granular level, like how much time been spent on suspending layout vs iterating over columns and making the columns hide.Once you know the problematic area you can work on optimizing that one.I am assuming here the problem statement is going to be the time that been spent on iteration as we have 300 columns and checking type of each one and hiding based on their type.

Upvotes: 2

Related Questions