4

GOAL: Being able to check all check boxes upon firing an event handler.

CURRENTLY: Event handler fires as it should. I have a foreach loop that goes through each row and checks the check box in that row.

PROBLEM: If I were to select a row prior to 'Check All', all check boxes are checked EXCEPT for the check box in the row that was selected/highlighted. If I click somewhere else outside of the check box area after that, the check box then checks itself.

QUESTION: How do I make it so that ALL check boxes are checked at the SAME TIME regardless of which row is selected or not?

CODE:

foreach (DataGridViewRow row in mTargets.Rows)
{
    //row.Cells[(int) menuItem.Tag].Value is the check box
    //mDeselect is the boolean that I want to set
    row.Cells[(int) menuItem.Tag].Value = !mDeselect;
}

ATTEMPTS: I've tried clearing selections and suspending/resuming layout. I've also tried to research to see if others have had the same problem, but it's a topic that is hard to find.

Any elegant suggestions or references to solutions that could help is greatly appreciated! Thank you!


UPDATED WITH ELEGANT SOLUTION & EXPLANATION: The reason why this side effect happens is due to the fact that the DataGridViewCheckBoxCell thinks it's still in edit mode whenever you happen to select the cell (or as it seems, that you're selecting that row). To solve the problem, here is the code that I put before my foreach loop that helped me fix the issue:

if (mTargets.IsCurrentCellInEditMode)
{
    mTargets.EndEdit();
}
susieloo_
  • 1,391
  • 1
  • 16
  • 22

2 Answers2

3

The reason why this side effect happens is due to the fact that the DataGridViewCheckBoxCell thinks it's still in edit mode whenever you happen to select the cell (or as it seems, that you're selecting that row). To solve the problem, here is the code that I put before my foreach loop that helped me fix the issue:

if (mTargets.IsCurrentCellInEditMode)
{
    mTargets.EndEdit();
}
susieloo_
  • 1,391
  • 1
  • 16
  • 22
0

I have had this problem before too. It is not that it does not set that value, it just does not paint correctly. The way i fixed it (by far not idea, but works) is to set the check box values, then change the selected row to another row, then change it back.

Jeffrey Wieder
  • 2,336
  • 1
  • 14
  • 12
  • That certainly requires less work to fix. I will try this too. But how is this different from me clearing the selection? To me, it seems like the same thing. I'm just so puzzled why this is happening because I'm pretty sure we're not the only ones who have experienced this before. 'Select All' is a popular functionality. – susieloo_ Sep 12 '14 at 17:00
  • 1
    DataGridView is a very complex control, there are a TON of very strange things it does because of it. This is likely a small bug in the repaint code that just does not repaint check boxes correctly. Clearing the selection is not the same as changing the selection to a DataGridView, you would think it would be similar, but somewhere deep inside the control it is working differently. – Jeffrey Wieder Sep 12 '14 at 17:04
  • I tried this, and it didn't work for me. :( It works half the time depending on where I change the selections. – susieloo_ Sep 12 '14 at 17:33
  • If it only worked half the time it is likely a timing issue, try putting a short Thread.Sleep before re-selection. Start small like 100ms and increase to see if you can get consistent results. – Jeffrey Wieder Sep 12 '14 at 17:37
  • That didn't work. Back to original behavior. So weird! **EDIT**: Actually, it's still works half the time. – susieloo_ Sep 12 '14 at 17:45
  • Thanks for trying to help, I really appreciate it! – susieloo_ Sep 12 '14 at 17:48
  • I've updated my post with the solution! Hope this will help you in the future! :) – susieloo_ Sep 12 '14 at 21:44