Solved

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

Posted on 2008-10-07
2
149 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
2 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 250 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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 …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now