?
Solved

How do I validate a value in a databound DataViewGrid in vb.net?

Posted on 2008-10-07
2
Medium Priority
?
156 Views
Last Modified: 2012-05-05
I am trying to trap entry errors in a databound DataGridView.  I am trying to stop the user from entering non-numeric values in a numeric field.  I'm able to trap the error in the CellValidating event.  But, it still generates a DataGridView error for a System.Format exception.  How do a trap the error and make sure it doesn't get to the CellValueChanged event?  I just want a simple solution.  I seem to be running around in circles.
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        Dim sUnitOld As String
        Dim sUnitNew As String
 
        'UNIT
        If (DataGridView1.Columns(e.ColumnIndex).Name = "UNIT") Then
            sUnitOld = DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value.ToString
            sUnitNew = e.FormattedValue.ToString
            bUnitVerified = True
            If Not IsNumeric(sUnitNew) Then
                bUnitVerified = False
                MsgBox("Unit value '" & sUnitNew & "' is not numeric. Please correct before saving.", , "Invalid Unit")
                DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = sUnitOld
                Exit Sub
            End If
 
        End If
 
   End Sub

Open in new window

0
Comment
Question by:tjlemahieu
[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
2 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 1000 total points
ID: 22664481
You can use the DataError event.
Example:

    Private Sub DataGridView1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        If e.ColumnIndex = 0 Then
            MessageBox.Show(e.Exception.Message)
            e.Cancel = True ' Disables the system error message
        End If
    End Sub

Open in new window

0
 

Author Closing Comment

by:tjlemahieu
ID: 31503980
That worked great!  Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 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