我有一个程序,它看起来像这样
请注意选中带红色和红色的值。
让我说我会点击一个单元格(checkboxcolumn)
如果我点击一个单元格,Msgbox将首先显示Are you you want to update changes?
如果我点击Yes
,程序将检查其当前值并更新类似的内容。
If value = yes then
value = No
elseif value = no then
value = Yes
end if
如果我在msgbox中选择NO
,则当前单元格的值将保持不变。
这是我的代码
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
答案 0 :(得分: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
确保为数据单元而不是标题单元处理事件。