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.
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
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
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
but when I put use this I need to click a few times on a CheckboxYou 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.
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.
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.RowsI believe you need to EXCLUDE the current row in the code snipped above:
row.Cells(e.ColumnIndex).Value = False
Next
For Each row As DataGridViewRow In Me.DataGridView1.Rows
If row.Index <> e.RowIndex Then
row.Cells(e.ColumnIndex).V
End If
Next
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers 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.