I have a program and it looks like this
please be focused on the value the one with checked and color red.
Lets say I will click a cell (checkboxcolumn)
if I click a cell a Msgbox will show first sayon Are you you want to update changes?
If I click Yes
the program will check its current value and Update it something like this.
If value = yes then
value = No
ElseIf value = no then
value = Yes
end if
and if I select NO
in the msgbox the value of the current cell will remains the same.
here is my code
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
My Question is.
How can I check the the value of cell and turn it vice-versa when I click Yes in the messagebox? and Remains the value or return to its original value when I click NO in the messagebox.
I try my code above but it looks not working
TYSM
You can use such criteria:
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
In above code I showed a confirm message to the user and the if the user choose Yes
, I reverted the value of cell.
In the above code I used e.ColumnIndex = 0
to show the confirm for first column. You may need some other criteria, for example e.ColumnIndex = 1
. Or as another example (e.ColumnIndex >=1 AndAlso e.ColumnIndex <=13)
.
e.RowIndex >= 0
Makes sure the event is handled for a data cell and not a header cell.