DataGridView Cell Validation?

Using winform and VB.NET and DataGridView.
I have code behind the CellValidating. But it does not seem to get invoked when navigating to another Cell. (Code Behind the RowValidating does get triggered when navigating to different row.) This is not consistant. I can tab to the next cell and the code is not called, then tab back back to the cell (that has the validating), then tab to the next cell and this time it does call the code.
How is this suppose to work? There is an e.cancel property that when it is set to true is suppose to keep the focus in the cell but if the code is not invoked what good is that?
LVL 1
Jess31Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
can you please post the code (Cell Validating)?
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
what kind of validation do you want to achieve?
0
Jess31Author Commented:
I would like that the user entering an Amount greater than n be alerted and unless the user choose to explicitly do this that he/she will be prvented from leaving this cell.
Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) _
                Handles DataGridView1.CellValidating

        If ynLoading = False Then
            If Me.DataGridView1.Columns(e.ColumnIndex).Name = "Receiving" Then
                If CInt(Me.DataGridView1.Rows(e.RowIndex).Cells("Receiving").Value) > CInt(Me.DataGridView1.Rows(e.RowIndex).Cells("Backordered").Value) Then
                    If MsgBox("Are you sure you wish to Over-Receive, to Receive more than what appears on the Purchase Order?", _
                              CType(MsgBoxStyle.Exclamation + MsgBoxStyle.YesNoCancel, MsgBoxStyle), "Is the Amount in Error") _
                              <> MsgBoxResult.Yes Then
                        e.Cancel = True
                        Exit Sub
                    End If
            End If
end if
End sub

Open in new window

0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Jess31Author Commented:
please ignore this line
>  If ynLoading = False Then
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Remove initially your messagebox to see if it works without it
Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) _
                Handles DataGridView1.CellValidating

        If ynLoading = False Then
            If e.ColumnIndex)= "YourColumnIndex" Then   '----->Change this 
                If CInt(Me.DataGridView1.Rows(e.RowIndex).Cells("Receiving").Value) > CInt(Me.DataGridView1.Rows(e.RowIndex).Cells("Backordered").Value) Then
                        e.cancel = True
                        Exit Sub
                    End If
            End If
        End if
End sub

Open in new window

0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
I thing that the cell validating event validates the cell it self...
you may add validation after the completion of entry before you update your data ...it much easier ...and you dont force any time ..the user with canceling his entries...
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
YOu put this code before your update method
 For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            If IsDBNull(Me.DataGridView.Rows(i).Cells("Receiving").Value) Or _
            IsDBNull(Me..Rows(i).Cells("Backordered").Value) Then
               MessageBox.Show("...................................", "Empty cells", MessageBoxButtons.OK, MessageBoxIcon.Warning) 
               Exit Sub
            ElseIf If CInt(Me.DataGridView1.Rows(i).Cells("Receiving").Value) > CInt(Me.DataGridView1.Rows(i).Cells("Backordered").Value)  Then
                
                MessageBox.Show("...................................", "......", MessageBoxButtons.OK, MessageBoxIcon.Warning)
          
                Exit Sub

          End If
 Next 
' your update method ...this is one option

Open in new window

0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Also you may try your code in the row Validating event
0
ZhaolaiCommented:
Try this:


        If e.ColumnIndex = youcolumnindex Then
            If CInt(e.FormattedValue) > CInt(Me.DataGridView1.Rows(e.RowIndex).Cells("Backordered").FormattedValue) Then
                If MsgBox("Are you sure you wish to Over-Receive, to Receive more than what appears on the Purchase Order?", _
                          CType(MsgBoxStyle.Exclamation + MsgBoxStyle.YesNoCancel, MsgBoxStyle), "Is the Amount in Error") _
                          <> MsgBoxResult.Yes Then
                    e.Cancel = True
                    Exit Sub
                End If
            End If
        End If

Open in new window

0
ZhaolaiCommented:
Oh... of cause in your CellValidating event. Complete code:

    Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        If e.ColumnIndex = YourColumnIndex Then
            If CInt(e.FormattedValue) > CInt(Me.DataGridView1.Rows(e.RowIndex).Cells("Backordered").FormattedValue) Then
                If MsgBox("Are you sure you wish to Over-Receive, to Receive more than what appears on the Purchase Order?", _
                          CType(MsgBoxStyle.Exclamation + MsgBoxStyle.YesNoCancel, MsgBoxStyle), "Is the Amount in Error") _
                          <> MsgBoxResult.Yes Then
                    e.Cancel = True
                    Exit Sub
                End If
            End If
        End If
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
It is a little strange ..in the cell validating.....he must also check for empty cells ...
Also to make a comparrison with cell validating event -in this case - the Backorderd must be already filled
0
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
just to be sure that Backordered is already filled try ...
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If ynLoading = False Then
       If e.columnIndex="YourColumnIndex"
          If Not IsDBNull(Me.Me.DataGridView1.Rows(e.RowIndex).Cells("Backordered").Value) Or Me.DataGridView1.Rows(e.RowIndex).Cells("Backordered").Value Is Not Nothing then
                If Me.DataGridView1.Columns(e.ColumnIndex).Name = "Receiving" Then
                    If CInt(Me.DataGridView1.Rows(e.RowIndex).Cells("Receiving").Value) > CInt(Me.DataGridView1.Rows(e.RowIndex).Cells("Backordered").Value) Then
                        If MsgBox("Are you sure you wish to Over-Receive, to Receive more than what appears on the Purchase Order?", _
                                  CType(MsgBoxStyle.Exclamation + MsgBoxStyle.YesNoCancel, MsgBoxStyle), "Is the Amount in Error") _
                                  <> MsgBoxResult.Yes Then
                            e.Cancel = True
                            Exit Sub
                        End If
                    End If
                End If
            End If
         End If

Open in new window

0
Jess31Author Commented:
e.FormatedValue is key.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.