Link to home
Start Free TrialLog in
Avatar of Jess31
Jess31

asked on

DataGridView / CheckBox Column

using vb.net and DataGridView with a CheckBox Column.
How can I prevent two rows from being Checked, so that if I Check one row I want any other one that is Checked to be unchecked?

I tried this code that I found but when I put use this I need to click a few times on a Checkbox, I wan to be able to just click once.
 Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        'Check the column index and if the check box is checked.
        If e.ColumnIndex = CheckColIndex Then
            Dim isChecked As Boolean = CType(Me.DataGridView1(e.ColumnIndex, e.RowIndex).Value, Boolean)
            If isChecked Then
                'If check box is checked, uncheck all the rows, the current row would be checked later.
                For Each row As DataGridViewRow In Me.DataGridView1.Rows
                    row.Cells(e.ColumnIndex).Value = False
                Next
            End If
        End If
    End Sub

Open in new window

Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Why not use radio button instead, if possible, which is more appropriate control for the purpose?
I tried this code that I found but when I put use this I need to click a few times on a Checkbox, I wan to be able to just click once.
I can clicked the checkbox with just one click.

and you can add condition:

e.RowIndex > -1

Open in new window


to your code

Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        'Check the column index and if the check box is checked.
        If e.ColumnIndex = CheckColIndex And e.RowIndex > -1 Then
            Dim isChecked As Boolean = CType(Me.DataGridView1(e.ColumnIndex, e.RowIndex).Value, Boolean)
            If isChecked Then
                'If check box is checked, uncheck all the rows, the current row would be checked later.
                For Each row As DataGridViewRow In Me.DataGridView1.Rows
                    row.Cells(e.ColumnIndex).Value = False
                Next
            End If
        End If
    End Sub

Open in new window

but when I put use this I need to click a few times on a Checkbox
You may need to change the EditMode of the DataGridView to EditOnEnter (or whatever setting makes sense to your program).

Otherwise I believe Ryan has you sorted.
Avatar of Jess31
Jess31

ASKER

Nitin
Can't use radio buttons in DataGridview.

Ryan,
I don't know where you are suggesting to add this.

Scot,
There is another and bigger problem with this code is that it doesn't work: as soon as I change focus to another control the DataGridView Checkbox that was Checked is gets Unchecked.
I don't know where you are suggesting to add this.
I have added that into CellValueChanged event in my previous comment. try to check that.
For Each row As DataGridViewRow In Me.DataGridView1.Rows
    row.Cells(e.ColumnIndex).Value = False
Next
I believe you need to EXCLUDE the current row in the code snipped above:

For Each row As DataGridViewRow In Me.DataGridView1.Rows
  If row.Index <> e.RowIndex Then  
    row.Cells(e.ColumnIndex).Value = False
  End If
Next
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.