WSC
WSC

Reputation: 992

Toggle DataGridView checkbox column on ColumnHeaderMouseClick

I have a DataGridView dgv which is has a checkbox column chkColumn as the first column and is then populated with other columns via an SQL query.

I want to be able to click chkColumn header and toggle the checkboxes for all rows.

My current code is this, however clicking the header completes the action correctly except for any selected cell/row:

Private Sub chkColumn_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgv.ColumnHeaderMouseClick

        Dim checkCount As Integer = 0
        Dim rowsCount As Integer = 0

        For Each row As DataGridViewRow In dgv.Rows
            rowsCount = rowsCount + 1
            If row.Cells("chkColumn").Value = 1 Then
                checkCount = checkCount + 1
            End If
        Next

        If checkCount < rowsCount Then
            For Each row As DataGridViewRow In dgv.Rows
                row.Cells("chkColumn").Value = 1
            Next
        Else
            For Each row As DataGridViewRow In dgv.Rows
                row.Cells("chkColumn").Value = 0
            Next
        End If

    End Sub

Upvotes: 0

Views: 519

Answers (1)

JohnG
JohnG

Reputation: 9469

I question your comment… ”clicking the header completes the action correctly except for any selected cell/row:” … I will agree with the part about the “selected” cell not toggling. However, I disagree with “completes the action correctly.”

My test running the posted code shows that when “any” column header is clicked the “first” time, all the check boxes will be set to checked. Then, any subsequent clicks of “any” column will simply toggle “all” rows to the same value. Meaning after the first time the column is clicked, all check boxes will be the same value. It appears you may be making this more complicated than it has to be.

The issue in relation to the “selected” cell not getting toggled is correct. Obviously, it appears that if the cell is selected, its value isn’t getting toggled. A simple solution may be to “disable” the grid before this process and “enable” it after the toggling is done. This is dependent on if there are other possible events “looking” for a cell value change etc.…

Without detailing what the checkCount and rowsCount variables are doing, I feel both are unnecessary for the goal. In addition, as I stated earlier the current OP code is “toggling” the values when “any” column is clicked. I can only assume this is not what is wanted.

One possible solution is posted below. First a check is made to make sure the correct “column” (chkColumn) is clicked. If the “chkColumn” is clicked the DataGridView dgv is temporarily disabled and a loop starts through all the rows in the grid. A simple solution to “toggle” a Boolean is to take its negation. This is what the code below does… each check box’s value is set to the “opposite” of what its current value is.

A possible problem with this approach could come from “how” the grid gets its data. For some data sources, you may need to use a different approach. Hope that helps.

Private Sub dgv_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgv.ColumnHeaderMouseClick
    If (dgv.Columns(e.ColumnIndex).Name = "chkColumn") Then
        dgv.Enabled = False
        For Each row As DataGridViewRow In dgv.Rows
            If (Not row.IsNewRow) Then
                row.Cells("chkColumn").Value = Not (row.Cells("chkColumn").Value)
            End If
        Next
        dgv.Enabled = True
    End If
End Sub

Upvotes: 1

Related Questions