0

I have a code here but it's not working. What I'm trying to do is when the message box appeared and ask if i want to proceed and choose no then the selected checkbox will be change the value into false.

If e.ColumnIndex = 0 Then
        If e.RowIndex >= 0 And e.RowIndex <= Me.dgDispatchPosting.RowCount - 1 Then
            If Me.dgDispatchPosting.Item("Approve_Status", e.RowIndex).Value <> "No Action" Then
                If Me.dgDispatchPosting.Item("chkSelect", e.RowIndex).Value = False Then
                    If MsgBox("This Dispatch Code has been " & vbCrLf & "Do you want to proceed? " & Me.dgDispatchPosting.Item("Approve_status", e.RowIndex).Value & "!", vbQuestion + vbYesNo, "Courier Dispatch Summary ") = MsgBoxResult.No Then
                        Me.dgDispatchPosting.Item("chkSelect", e.RowIndex).Value = False 'Must change the value here (Uncheck)
                    End If
                End If
            End If
        End If
    End If
Osama Rizwan
  • 615
  • 1
  • 7
  • 19
  • 1
    On the line before the message box you check if `chkSelect` is false. Then you display the message box and if they hit no then you set it to `false` again. I suspect you're wanting to change line before MsgBox to check if it's true? Otherwise there is nothing for the code to do. – FloatingKiwi Oct 13 '16 at 01:45
  • The code is under _CellContentClick, the the line before message box is checking if the clicked/ selected checkbox is unchecked..if the selected checkbox is not yet checked the message box will pop but if the checkbox is checked already the message box will pop up..Then when i hit NO the clicked/checked checkbox will be turn to false. – Cristian Raymart Jacob Oct 13 '16 at 02:02
  • Your last comment doesn't make sense. You say "if the selected checkbox is not yet checked the message box will pop" then you say " if the checkbox is checked already the message box will pop up" According to your code the only way the message box will popup is it the value of `chkSelect` is false. Then you set it straight back to false, which is its current value. – FloatingKiwi Oct 13 '16 at 02:09
  • You might also be running into problems with the event timing. I'm not sure when the value of a checkbox column gets updated compared to when the cell click event fires. Try move it to `CellValueChanged`. – FloatingKiwi Oct 13 '16 at 02:10
  • Apologies, the message box will pop up if the clicked checkbox is false..all i want is when the message box pop up and i hit no..the checked checkbox will turn to false. – Cristian Raymart Jacob Oct 13 '16 at 02:14
  • When i transferred the code under CellValueChanged the message box is not triggered. – Cristian Raymart Jacob Oct 13 '16 at 02:18
  • It's already false so you don't need to set it to false again. I suspect it's a timing issue and that the checkbox is being checked after your cell click. Change the event handler you are using. – FloatingKiwi Oct 13 '16 at 02:18
  • Yes, its already false..but when i click the checkbox it will turn to true..then the message box will pop up..so, when i hit the NO the checkbox should be turn to false..why is that the check box not turning to false or not displaying as unchecked..This code Me.dgDispatchPosting.Item("chkSelect", e.RowIndex).Value = False is not turning the checkbox into false. – Cristian Raymart Jacob Oct 13 '16 at 02:30
  • I just tried creating a sample app and I see there are a lot of issues with the timing in a checkbox column. see here for potential solutions. http://stackoverflow.com/questions/11843488/datagridview-checkbox-event#15011844 – FloatingKiwi Oct 13 '16 at 03:10

1 Answers1

0

I've got the answer on how to unchecked the checkbox in datagridview. I just select/focus in the other column/row after changing the checkbox value.

If Me.dgDispatchPosting.Item("chkSelect", e.RowIndex).Value = True Then
   If MsgBox("This Dispatch Code has been " & Me.dgDispatchPosting.Item("Approve_status", e.RowIndex).Value & "!" & vbCrLf & "Do you want to proceed? ", vbQuestion + vbYesNo, "Courier Dispatch Summary ") = MsgBoxResult.No Then
      'Must change the value here (Uncheck)'Must change the value here(Uncheck)
      Me.dgDispatchPosting.Item("chkSelect", e.RowIndex).Value = False 
      'After Changing the value you must select/ focus in the other column or row to take effect the changes you made in code.
      Me.dgDispatchPosting.Item("Approve_status", e.RowIndex).Selected = True 
   End If
End If
Osama Rizwan
  • 615
  • 1
  • 7
  • 19