Question

I have been struggling with CellFormatting event, it's so slow.

I have a DataGridView something like this:

enter image description here

I have written a function that fires when you click the checkbox in the header and it makes all the check boxes to check in that column....

private void checkboxHeader_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.RowCount; i++)
        {
            dataGridView1[0, i].Value = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]).Checked;
        }
        //dataGridView1.EndEdit();
    }  

And this function is working when I have something like 10 rows it works perfectly, but when I have 300 rows something that I should have... there is a something like 9 seconds delay for making all the checkboxes checked, and I found out that it's due to CellFormating event.

My CellFormating event code is:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            DataGridViewCellStyle _myStyle = new DataGridViewCellStyle();
            int index = gdv_row.FindIndex(p => p.log == (string)dataGridView1.Rows[e.RowIndex].Cells[1].Value);
            if (index != -1 && dataGridView1.Columns[e.ColumnIndex] is DataGridViewTextBoxColumn && e.RowIndex != -1)
            {
                //e.CellStyle = _myStyle;
                _myStyle.Font = gdv_row[index].font;
                _myStyle.BackColor = gdv_row[index].backgroundcolor_color;
                _myStyle.ForeColor = gdv_row[index].foregroundcolor_color;
                dataGridView1.Rows[e.RowIndex].Cells[1].Style = _myStyle;
            }
        }

and I have used DoubleBuffering for DataGridView. Now I don't have any idea what should I do with this CellFormatting event...

Was it helpful?

Solution

Did you already try SuspendLayout() and ResumeLayout()?

This temporarily suspends the layout logic for the control, so that there is no redraw of the grid while you are populating it.

If you use DoubleBuffering the Grid still redrows itself which ist still slow. But if you do not redraw at all while you populate the Grid, this should give a dramatic impovement.

Your first function might then look like this:

private void checkboxHeader_CheckedChanged(object sender, EventArgs e)
    {
        dataGridView1.SuspendLayout();

        for (int i = 0; i < dataGridView1.RowCount; i++)
        {
            dataGridView1[0, i].Value = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]).Checked;
        }

        dataGridView1.ResumeLayout();
    }  

[Edit 1]

Added code sample.

[Edit 2] To minimise the necessary drawing of the rows, instead of creating a new DataGridViewCellStyle object for each row, try setting the properties of the existing style directly:

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        int index = gdv_row.FindIndex(p => p.log == (string)dataGridView1.Rows[e.RowIndex].Cells[1].Value);
        if (index != -1 && dataGridView1.Columns[e.ColumnIndex] is DataGridViewTextBoxColumn && e.RowIndex != -1)
        {
            dataGridView1.Rows[e.RowIndex].Cells[1].Style.Font = gdv_row[index].font;
            dataGridView1.Rows[e.RowIndex].Cells[1].Style.BackColor = gdv_row[index].backgroundcolor_color;
            dataGridView1.Rows[e.RowIndex].Cells[1].Style.ForeColor = gdv_row[index].foregroundcolor_color;
        }
    }

Finally, looking for some solution, I found this MSDN article document: Best Practices for Scaling the Windows Forms DataGridView Control

[EDIT 3] (Response to Ehsan's comment below)

This is because "a" is a value that is instantly there to display in the Grid while the original line does some significant work: * Performs a search for the desired value, including all child controls * Creates an Array with the found results * Makes a Cast from object to CheckBox * It does all of this per each and every single line in your Grid

It becomes obvious that this becomes more time consuming the more items you have in your DataGridView.

If I understood your code correctly it should help you to change the method into this:

  CheckBox headerBox = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]);
  for (int i = 0; i < dataGridView1.RowCount; i++)
  {
    dataGridView1[0, i].Value = headerBox.Checked;
  }

By doing this you only perform the search once.

OTHER TIPS

If you want to stop painting the control when you're checking all the rows, you should take a look at the DrawingControl class from this post: https://stackoverflow.com/questions/487661/...

In my case the problem was caused by DataGridView property AutoSizeColumnsMode=AllCells. Probably after cell formatting all other cells in column and header cell had to be redrawn to fit theirs dimensions to the new cell dimensions. After I was changed property value to it default value "None", grid is painted immediately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top