DataGridView - Assigning DefaultCellStyle for each row run extremely slow

214 Views Asked by At

I have a DataGridView bound to a DataTable with 5000 rows, 36 columns and I'd like to change the cell style of each row based on its content, but the function I made take an insanely long time to run (100 rows every 5 min).

This is my function:

private void formatRows()
{
      DataGridViewCellStyle GroupCellStyle = new DataGridViewCellStyle();
      DataGridViewCellStyle SubTotCellStyle = new DataGridViewCellStyle();

      GroupCellStyle.Font = new Font(dataGridView1.Font, FontStyle.Bold);
      GroupCellStyle.BackColor = Color.FromArgb(235, 235, 235);
      SubTotCellStyle.BackColor = Color.FromArgb(155, 155, 255);

      dataGridView1.SuspendLayout();
      for (int r = 0; r < dbInterface.ds.Tables["articles"].Rows.Count; r++)
      {
            if (dbInterface.ds.Tables["articles"].Rows[r].Field<short>("FLGGroup") == 2)
                  dataGridView1.Rows[r].DefaultCellStyle = GroupCellStyle;

            else if (dbInterface.ds.Tables["articles"].Rows[r].Field<short>("FLGGroup") == 0)
                  dataGridView1.Rows[r].DefaultCellStyle = SubTotCellStyle;
      }
      dataGridView1.ResumeLayout();
}

I can't figure out why it take so much time. Is that normal with DataGridView?

0

There are 0 best solutions below