I have a form with a dgv on it. The dgv has 1 column defined as a DataGridViewCheckBoxColumn. I would like this dgv to behave as a radiobutton group, i.e. when I click on one cell, that should be set to true, and the others to false.

I've got it to work fine ... as long as the user does not click twice on the same cell. When that happens, none of the checkboxes are set. It seems that after I assign the values in the for each loop, the current event is still executed (i.e. the current checkbox is changed into false).

    Private Sub DataGridView1_CellContentClick(
        sender As Object, 
        e As DataGridViewCellEventArgs) 
        Handles DataGridView1.CellContentClick
        For Each r As DataGridViewRow In DataGridView1.Rows
            If r.Index = e.RowIndex Then
                r.Cells(0).Value = True
            Else
                r.Cells(0).Value = False
            End If
        Next
    End Sub

I would not mind assigning the values after the system has proccessed the event althogether, but I cannot find an event that gets triggered.

The answer might be "hidden" here but I cannot make any sense out of what they explain, so if anybody could translate this into English :).

Typically, check box cell values are intended either for storage, like any other data, or for performing bulk operations. If you want to respond immediately when users click a check box cell, you can handle the DataGridView.CellContentClick event, but this event occurs before the cell value is updated. If you need the new value at the time of the click, one option is to calculate what the expected value will be based on the current value. Another approach is to commit the change immediately, and handle the DataGridView.CellValueChanged event to respond to it. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.

This is my "if you can't beat them join them" kind of solution, but there has got to be a more elegant solution to this:

Private Sub DataGridView1_CellContentClick(
        sender As Object, 
        e As DataGridViewCellEventArgs) 
        Handles DataGridView1.CellContentClick
        For Each r As DataGridViewRow In DataGridView1.Rows
            If r.Index = e.RowIndex Then
                r.Cells(0).Value = True
                r.Cells(0).ReadOnly = True
            Else
                r.Cells(0).Value = False
                r.Cells(0).ReadOnly = False

            End If
        Next
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.