1
votes

I have a datagridview (here called cameraTable). One column of the datagridview is purely checkboxes. I have a button that when clicked will check all the boxes in the column, aka select all. Here's the code:

Private Sub btnSelectAll_Click(sender As Object, e As EventArgs) Handles btnSelectAll.Click

    For Each row As DataGridViewRow In cameraTable.Rows
        DirectCast(row.Cells("CheckBox"), DataGridViewCheckBoxCell).Value = _
            True
    Next
End Sub

The problem I'm having is that the initial box (i.e. row zero) never changes to checked. I tried these:

DirectCast(cameraTable(0, 0), DataGridViewCheckBoxCell).Value = True ' doesn't work 
cameraTable.Rows(0).Cells(0).Value = True ' doesn't work 

Neither change the box to checked, but the other rows within the column change without a problem. I even did a msgbox that prints out the value, and turns out to be "True". So the value of the checkbox has changed, but the check box in the datagridview is still unchecked. Any ideas?

Thanks for all the help!

-K

3
Hmm, turns out it's directly correlated to whatever row I have blue/selected naturally by the mouse. Is there a way to remove that selection via the program?Kat
Call the DataGridView's ClearSelection() methodstuartd
Thanks stuartd! I just tried that (camertable.clearselection()), but still no box value change. Hmm.Kat
Are you sure not setting false on somewhere after setting true??Jumpei
ClearSelection isn't the best solution, it's better to set currentcell property to cell other than the one with checkbox (Dan's answer)solujic

3 Answers

1
votes

The datagrid will not get INotifyPropertyChanged events (or will not process them) for the row/cell that is currently selected. If you have other columns that can be edited, move the focused column to it.

If all else fails, reload the grid after seting the values by setting the datasource = nothing and then back to what it was.

1
votes

Steve is right. What I did was when the check box is clicked, I set the focus to another cell on that row (my dgv is set to FullRowSelect):

Private Sub dgv_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellContentClick
  If e.ColumnIndex = iCBColIDX Then
    dgv.CurrentCell = dgv.Rows(e.RowIndex).Cells("sts")
  End If
End Sub
1
votes

Make your checkbox column width=20 instead of 18.

No, I don't know why, but it helps.

Thanks this man.

PS. It isn't a joke, it's a bug.