-1

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

在此处输入图像描述

请关注带有选中和红色的值。

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

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

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

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

如果我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

我的问题是。

当我在消息框中单击“是”时,如何检查单元格的值并反之亦然?当我在消息框中单击“否”时,保持该值或返回其原始值。

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

TYSM

4

1 回答 1

1

您可以使用这样的标准:

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确保为数据单元格而不是标题单元格处理事件。

于 2016-09-17T14:24:27.130 回答