Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello my friends:

I have a DataGridView in Windows Form. The First column contains check boxes.
I am able to determine if any of the boxes are checked, however, I need to know if all are unchecked.

For example:
If a user clicked on any of the check box, then do something, if the user unchecked all of the previously checked check boxes do something else.

Again, I am able to determine if a box is checked or unchecked. However, what I need is to determined is when any box is checked and when ALL boxes are unchecked.

I need this to happen each time a row is check so that I can know when the condition is met. Meaning I am not using a Button event.

What I have tried:

The following Code returns a Bool each time the user checks or unchecks a check box on the grid. This does not work because I want to know when ANY boxes are checked and if All are unchecked.

For example: Lets say the user checks 5 boxes. I only want to be notified once. And notified again when all 5 are unchecked not just the individual boxes.

VB
Public Function IsContainChecked() As Boolean

    Dim ch1 As New DataGridViewCheckBoxCell()
    ch1 = DirectCast(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0),  _
        DataGridViewCheckBoxCell)
    Return CBool(ch1.EditingCellFormattedValue)

End Function
Posted
Updated 18-Sep-17 11:38am
v3
Comments
Graeme_Grant 18-Sep-17 20:49pm    
That is the way that it works. You need to write code to handle the condition the way that you want it to.
j snooze 19-Sep-17 17:30pm    
instead of doing boolean, why not keep a counter that you add and subtract from on each check/uncheck. When its zero you know they are all unchecked, when its not you know that at least one checkbox is checked. I guess I don't know your requirements, so this is just a suggestion that may not suit your needs, but is a way to track if no boxes are checked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900