Solved

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

Posted on 2008-10-07
2
151 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Advice in Xamarin 21 79
I need help making a collection from a class in VB.Net 3 33
Crystal reports vb.net 2 41
vb.net help 4 31
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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