Datagridview and checkbox - strange behavior.

0

I have a datagridview with some checkboxes. I have this code:

Private Sub DataGridView2_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView2.CellMouseClick
    If e.ColumnIndex > 1 Then
        DataGridView2(e.ColumnIndex, e.RowIndex).Value = True
    End If

End Sub

But the checkbox is only "checked" when the focus exits. In other words, it is only "checked" when I click on another object in the project.

    
asked by anonymous 15.02.2016 / 18:32

2 answers

0

This happens because whenever you click on a cell, it is in a column with an index greater than 1:

If e.ColumnIndex > 1 Then 
    DataGridView2(e.ColumnIndex, e.RowIndex).Value = True
End If

Your code is causing the cell to always have the ckeckbox value true.

    
15.02.2016 / 20:59
0

I've solved it. I was using the action "cellvaluechanged" and this was not being called in due time.

The problem is that in vb when we use checkboxes and datagridviews, it only checks when we focus on another object.

To solve this, I just put a line of code where I focus on another object and that's it, checkboxes already work.

    
16.02.2016 / 10:49