Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7028105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:20:32+00:00 2026-05-28T00:20:32+00:00

I have been struggling with CellFormatting event, it’s so slow. I have a DataGridView

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:20:33+00:00Added an answer on May 28, 2026 at 12:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling for hours to input this change event to exexcute in
I have been struggling to think of some decent uses for things like vectors
I have been struggling with this question for awhile now, and I haven't reached
I have been struggling with this for quite some time having been accustomed to
I have been struggling to find an answer to this theoretical question, even tho
I have been struggling with this for months in my project. Here's the deal:
I have been struggling with this yesterday whole day, and still couldnt figure it
I have been struggling with the unholy alliance that is WebSphere 7+ and Mojarra
I have been struggling with this all day. I have an html table in
I have been struggling to make a set of reports that gives the sum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.