DataGridView CheckBox,用户确认后还原Value

DataGridView CheckBox, revert the Value after user confirmation

我有一个程序,它看起来像这样

请注意勾选红色的数值

假设我将单击一个单元格(复选框列)

如果我单击一个单元格,Msgbox 将首先显示 Are you you want to update changes?

如果我单击 Yes,程序将检查其当前值并像这样更新它。

If value = yes then
   value = No
ElseIf value = no then
   value = Yes
end if

如果我 select NO 在 msgbox 中,当前单元格的值将保持不变。

这是我的代码

If (e.ColumnIndex = 1 AndAlso e.RowIndex >= 0) Then
    Dim value = DirectCast(DataGridView1(e.ColumnIndex, e.RowIndex).FormattedValue, Nullable(Of Boolean))
    Dim result = MessageBox.Show("Are you sure to uncheck item?", "", MessageBoxButtons.YesNoCancel)
    If (value.HasValue AndAlso value = True) Then
        If (result = System.Windows.Forms.DialogResult.Yes) Then
            If DataGridView1(e.ColumnIndex, e.RowIndex).Value = True Then
                DataGridView1(e.ColumnIndex, e.RowIndex).Value = False
            Else
                DataGridView1(e.ColumnIndex, e.RowIndex).Value = True
            End If
        ElseIf (result = System.Windows.Forms.DialogResult.No) Then
        End If
    Else
    End If
End If

我的问题是。

当我在消息框中单击“是”时,如何检查单元格的值,反之亦然?当我在消息框中单击“否”时,将值或 return 保留为其原始值。

我尝试了上面的代码,但它看起来不起作用

TYSM

您可以使用这样的条件:

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, _
    ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If (e.ColumnIndex = 0 AndAlso e.RowIndex >= 0) Then
        Dim value = DirectCast(DataGridView1(e.ColumnIndex, e.RowIndex).FormattedValue, _
                               Nullable(Of Boolean))
        Dim result = MessageBox.Show("Are you sure to change vaule?", "", _
                                     MessageBoxButtons.YesNoCancel)
        If (result = System.Windows.Forms.DialogResult.Yes) Then
            If (value.HasValue AndAlso value = True) Then
                DataGridView1(e.ColumnIndex, e.RowIndex).Value = False
            Else
                DataGridView1(e.ColumnIndex, e.RowIndex).Value = True
            End If
        End If
    End If
End Sub

在上面的代码中,我向用户显示了一条确认消息,如果用户选择 Yes,我将恢复单元格的值。

在上面的代码中,我使用 e.ColumnIndex = 0 来显示第一列的确认。您可能需要一些其他条件,例如 e.ColumnIndex = 1。或者作为另一个例子 (e.ColumnIndex >=1 AndAlso e.ColumnIndex <=13).

e.RowIndex >= 0 确保针对数据单元而不是 header 单元处理事件。