0

The first column in a DataGridView (dgv1) has checkboxes. Interestingly, when you click on the last checkbox desired, as soon a you loop through the rows for that column to evaluate checked items, the last cell checked (with a cursor on it) does not evaluate to true. The syntax for looping through the checkboxes in column 0 is below:

    Dim NumCBChecked As Integer = 0
    For i = 0 To dgv1.Rows.Count - 1
        If CBool(dgv1(0, i).Value) = True Then
            NumCBChecked += 1
        End If
    Next

Is there a way to set the focus to false if the cell containing the last checkbox checked is in edit mode?

BTW, refreshing the edit status (below) did not help before looping:

    dgv1.RefreshEdit()
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • 1
    Have you tried ... `dgv1.EndEdit()` ... or ... `dgv1.EndEdit(DataGridViewDataErrorContexts.Commit)` ? – JohnG Sep 16 '21 at 21:00
  • Thanks - dgv1.EndEdit() worked. –  Sep 16 '21 at 21:08
  • [DataGridView CheckBox selection bug](https://stackoverflow.com/a/63693094/7444103) -- [Programmatically check a DataGridView CheckBox that was just unchecked](https://stackoverflow.com/a/62031996/7444103) -- [Bound DataGridView not updating to display information + sorting issues](https://stackoverflow.com/a/59006605/7444103) – Jimi Sep 16 '21 at 21:48
  • 1
    Please don't leave questions unanswered. If you worked out the solution based on comments then add your own answer and accept it, so that we can see that you don't need any more help without wasting our time opening your question and reading it, only to find a comment that says it's resolved. – jmcilhinney Sep 17 '21 at 00:47
  • thx - that's a great suggestion. Done. –  Sep 17 '21 at 02:22

1 Answers1

0

Per @JohnG's comment, the suggestion worked as follows:

dgv1.EndEdit()
Dim NumCBChecked As Integer = 0
For i = 0 To dgv1.Rows.Count - 1
    If CBool(dgv1(0, i).Value) = True Then
        NumCBChecked += 1
    End If
Next