当用户取消选中 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
最佳答案
尝试调用 RefreshEdit()
而不是 Invalidate()
,如下所示:
Else
row.Cells(0).Value = True
DataGridView2.RefreshEdit()
End If
关于vb.net - 以编程方式切换 DataGridView 中的 CheckBox 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10901761/