0

I currently have an issue with checking and unchecking a lot of checkboxes in a datagridview. I already run them parallel but its still really slow, even loading the rows in is faster...

EDIT: Issue is fixed. The AutoSizeColumnsMode and AutoSizeRowsMode caused it to slow down!

if (checkBox_all.Checked)
{
    Parallel.ForEach(dataGrid_searchEntryList.Rows.Cast<DataGridViewRow>(), row =>
    {
        row.Cells[0].Value = true;
    });
}
else
{
    Parallel.ForEach(dataGrid_searchEntryList.Rows.Cast<DataGridViewRow>(), row =>
    {
        row.Cells[0].Value = false;
    });
}

Any help is greatly appreciated!

Picture of the gridview

DeathRGH
  • 3
  • 4
  • 1
    `DataGridView` is not thread-safe. You can't use `Parallel.ForEach`. https://stackoverflow.com/questions/45089128/how-to-change-the-cell-value-for-entire-datagridview-column may be of interest as an approach to try. – mjwills Jun 25 '18 at 01:36
  • 1
    is it data bound? if so, try to update the objects instead – Ctznkane525 Jun 25 '18 at 01:38
  • @JohnG no just this one task. – DeathRGH Jun 25 '18 at 01:41
  • @mjwills I also want to implement it so that you can check the boxes by names in the 3rd cell and invert the check. I will add a pictrue to op in a second. – DeathRGH Jun 25 '18 at 01:41
  • 1
    I can not imagine what kind of delay you are experiencing. My test took a little under a half a second to process 6001 rows. As Zero's answer states... you can not use Parallel with the `DataGridView` – JohnG Jun 25 '18 at 01:55
  • @JohnG for me it takes over 10 seconds. Could it be due to a lot of other cells in the grid view ? I have 6 collums. – DeathRGH Jun 25 '18 at 02:02
  • I doubt if the number of columns will make a difference. In addition, it appears to take LESS time when the grid does not have a data source. Without a data source and 7 Columns, 6001 rows it took 0.0029074 seconds to change all the check boxes. – JohnG Jun 25 '18 at 02:11
  • @JohnG I fixed my issue, I updated op with it! – DeathRGH Jun 25 '18 at 02:16
  • You may also want to consider [double-buffering the dgv.](https://stackoverflow.com/questions/41893708/how-to-prevent-datagridview-from-flickering-when-scrolling-horizontally/41894210?s=1|53.9115#41894210) – TaW Jun 25 '18 at 06:15
  • @DeathRGH You really should try my solution. Your fix is a work-around for excessive drawing. You don't need to modify auto-sizing if you suspend drawing while you update. – Zer0 Jun 25 '18 at 21:05

2 Answers2

0

First off you cannot access WinForms controls outside of the GUI thread. So you need to eliminate the Parallel calls.

Oddly, DataGridView does not have BeginUpdate/EndUpdate. But you can suspend drawing:

dataGrid_searchEntryList.SuspendDrawing();
//Update multiple cells here
dataGrid_searchEntryList.ResumeDrawing();

This at least eliminates excessive drawing.

Worth noting that if this is databound there's a much better way of doing this. But I'm presuming it's not as you're accessing data via the DataGridView itself.

Finally your code can be simplified to just this:

foreach(var row in dataGrid_searchEntryList.Rows.Cast<DataGridViewRow>())
{
    row.Cells[0].Value = checkBox_all.Checked;
}
Zer0
  • 7,191
  • 1
  • 20
  • 34
0

Issue is fixed. The AutoSizeColumnsMode and AutoSizeRowsMode slowed it down!

DeathRGH
  • 3
  • 4