在DataGridView中以编程方式隐藏多列非常缓慢,耗时

原因:大概率DataGridView的AutoSizeRowMode设置是AllCells

解决:

AutoSizeRowMode设置为DisplayedHeaders解决了这个问题。 大约需要1秒。 在设置该值之前,该属性被设置为AllCells

datagridview.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedHeaders;

 案例:

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;
        }
    }

 

你可能感兴趣的:(winform,winform,datagridview,耗时)