Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlServer no dupes 25 38
.NET 2008 VB and C# 6 39
Help with preventing selection from a combobox 11 34
FInd Image Control Gridview 3 22
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 …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 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