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();
}