3

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...

Community
  • 1
  • 1
Ehsan
  • 4,334
  • 7
  • 39
  • 59

3 Answers3

4

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.

Jens H
  • 4,590
  • 2
  • 25
  • 35
  • SuspendLayout is gonna help you when you're adding new objects on the form. According to MSDN, SuspendLayout `Temporarily suspends the layout logic for the control.` – Gabriel GM Jan 03 '12 at 12:25
  • Thanks for you response, could you please give an answer in more details or sth like that because I don't know how to use this functions and where? – Ehsan Jan 03 '12 at 12:33
  • Actually I did what you said but there is still that delay even I see more delay! but what should I do? do you think maybe dataGridView1[0,i] has some error and it makes the delay?:( – Ehsan Jan 03 '12 at 13:44
  • The odd thing is when after sth like 10 seconds all row become selected when I'm changing all the BackgroundColor by that button in the right bottom of the Form it is done in a sec, but why that checkbox take 10 sec? – Ehsan Jan 03 '12 at 13:55
  • I think I found the problem but I can't solve it, the problem is dataGridView1[0, i].Value = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]).Checked; this line when I change the line to dataGridView1[1, i].Value = "a"; this means the log column should become "a" and it becomes instantly! I don't know the problem – Ehsan Jan 03 '12 at 14:12
  • 1
    When you get a control using a function, you should always store it into a variable if you're gonna use it more than once (memory is not gonna be a problem for a combobox). – Gabriel GM Jan 03 '12 at 17:38
  • 1
    @Sensei76: in your 3rd edit, it should be CheckBox headerBox = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]); (Remove the ".Checked" at the end) – Gabriel GM Jan 03 '12 at 17:39
  • Finally I found the problem I have used a binding list tuples for my data gridview and in the for statement I'm using the direct addressing so I added a field to the tupel class for the checkbox and address it like gdv_row[i].check = headerBox.Checked; and now it works fine! thanks for all your answers – Ehsan Jan 07 '12 at 09:36
0

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.

0

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/...

Community
  • 1
  • 1
Gabriel GM
  • 6,391
  • 2
  • 31
  • 34