Troy Mitchel
Troy Mitchel

Reputation: 1810

Toggle CheckBox Cell in DataGridView Programmatically

When user unchecks the checkbox for the datagridview row, messagebox displayed, if the user answers yes the row is deleted, if the user answers no then I need to re-check the check box. however it doesn't actually check unless i click anywhere on the datagridview afterwards. Any ideas to get the dgv to updated?

Private Sub DataGridView2_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellValueChanged

  If e.RowIndex <> -1 Then
    Dim row As DataGridViewRow = DataGridView2.Rows(e.RowIndex)
    If e.ColumnIndex = 0 Then
      If row.Cells(0).Value = False Then
        If MessageBox.Show("Delete view from database.", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
          DataGridView2.Rows.RemoveAt(e.RowIndex)
        Else
          row.Cells(0).Value = True
          DataGridView2.Invalidate()
        End If
      End If
    End If
  End If
End Sub

Private Sub DataGridView2_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView2.CurrentCellDirtyStateChanged
  If DataGridView2.IsCurrentCellDirty Then
    DataGridView2.CommitEdit(DataGridViewDataErrorContexts.Commit)
  End If
End Sub

Upvotes: 2

Views: 5946

Answers (1)

LarsTech
LarsTech

Reputation: 81610

Instead of Invalidate(), try calling RefreshEdit(), like this:

Else
  row.Cells(0).Value = True
  DataGridView2.RefreshEdit()
End If

Upvotes: 5

Related Questions