Answered by:
DataGridView CheckBox Column (Allow only 1 selection)

Question
-
I have a bound DGV with 1 'static' checkbox column in which I have added through the designer. I come up with this code to allow for only 1 row to be checked.
Private Sub EmpGrid_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles EmpGrid.CellContentClick Dim RowIndex As Integer = EmpGrid.CurrentRow.Index For Each row As DataGridViewRow In EmpGrid.Rows If Not row.Index = RowIndex Then row.Cells("SelCol").Value = False End If Next End Sub
When clicking on a chkbox for the first time it hangs for a few seconds, but after a box has already been checked, and after the short hang, it works as expected and no more hanging.
Any Ideas?
Live as if you were going to die today, learn as if you were going to live forever.
Wednesday, February 3, 2016 6:40 PM
Answers
-
There's no need to loop through every row. Set a module level variable to the current row index and initialise it at -1. Then you can clear the currently checked row without looping.
Dim CheckedRowIndex As Integer = -1
Private Sub EmpGrid_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles EmpGrid.CellContentClick
If CheckedRowIndex > -1 Then
EmpGrid("SelCol", CheckedRowIndex).Value = False
End If
CheckedRowIndex = e.RowIndex
End Sub
- Marked as answer by Gtripodi Wednesday, February 3, 2016 7:07 PM
Wednesday, February 3, 2016 7:03 PM -
Checking if the value is true before changing it to false has eliminated the short delay.
If Not row.Index = RowIndex Then If row.Cells("selcol").Value = True Then row.Cells("SelCol").Value = False End If
Live as if you were going to die today, learn as if you were going to live forever.
- Marked as answer by Gtripodi Wednesday, February 3, 2016 7:02 PM
Wednesday, February 3, 2016 7:02 PM
All replies
-
Checking if the value is true before changing it to false has eliminated the short delay.
If Not row.Index = RowIndex Then If row.Cells("selcol").Value = True Then row.Cells("SelCol").Value = False End If
Live as if you were going to die today, learn as if you were going to live forever.
- Marked as answer by Gtripodi Wednesday, February 3, 2016 7:02 PM
Wednesday, February 3, 2016 7:02 PM -
There's no need to loop through every row. Set a module level variable to the current row index and initialise it at -1. Then you can clear the currently checked row without looping.
Dim CheckedRowIndex As Integer = -1
Private Sub EmpGrid_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles EmpGrid.CellContentClick
If CheckedRowIndex > -1 Then
EmpGrid("SelCol", CheckedRowIndex).Value = False
End If
CheckedRowIndex = e.RowIndex
End Sub
- Marked as answer by Gtripodi Wednesday, February 3, 2016 7:07 PM
Wednesday, February 3, 2016 7:03 PM -
I see what you mean, your code is much more efficient.
Live as if you were going to die today, learn as if you were going to live forever.
Wednesday, February 3, 2016 7:08 PM -
Checking if the value is true before changing it to false has eliminated the short delay.
If Not row.Index = RowIndex Then If row.Cells("selcol").Value = True Then row.Cells("SelCol").Value = False End If
For the record. I am not real sure why, but I was getting varied results using this, until I converted the cell value to boolean.
If Convert.ToBoolean(row.Cells("selcol").Value) = True Then row.Cells("SelCol").Value = False
Think I will adopt Daves method...
Live as if you were going to die today, learn as if you were going to live forever.
Wednesday, February 3, 2016 7:26 PM