以编程方式在DataGridView中切换CheckBox单元格

时间:2012-06-05 17:12:51

标签: vb.net winforms

当用户取消选中datagridview行的复选框时,显示消息框,如果用户回答是,则删除该行,如果用户回答否,则需要重新选中该复选框。但是它实际上并没有检查,除非我之后点击datagridview上的任何地方。有什么想法让dgv更新?

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

1 个答案:

答案 0 :(得分:5)

而不是Invalidate(),请尝试调用RefreshEdit(),如下所示:

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