[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

DataGridView Cell Validation?

Posted on 2010-08-16
13
Medium Priority
?
924 Views
Last Modified: 2013-11-08
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?
0
Comment
Question by:Jess31
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 2
13 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33448651
can you please post the code (Cell Validating)?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33448684
what kind of validation do you want to achieve?
0
 
LVL 1

Author Comment

by:Jess31
ID: 33448745
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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 1

Author Comment

by:Jess31
ID: 33448751
please ignore this line
>  If ynLoading = False Then
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33448873
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33449085
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33449137
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33449167
Also you may try your code in the row Validating event
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33449173
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
 
LVL 17

Accepted Solution

by:
Zhaolai earned 2000 total points
ID: 33449198
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33449212
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33449548
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
 
LVL 1

Author Closing Comment

by:Jess31
ID: 33450295
e.FormatedValue is key.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question