Solved

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

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
VB.Net - BackgroundWorker running multiple 5 69
VB.net VSTO Excel Ribbon error 4 24
Web page design problem 3 37
Datatable / Dates ? 4 24
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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