?
Solved

FormatException Error Handling Issue

Posted on 2007-08-09
2
Medium Priority
?
575 Views
Last Modified: 2011-09-20
Hi all,

I have compiled the following code to hande errors on my winforms datagridview:

    Private Sub DG_platesNEW_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DG_platesNEW.CellBeginEdit

        cellno = DG_platesNEW.Rows(e.ColumnIndex).Index 'This holds the cellno incase the cell is left by the user.
        mSavedValue = DG_platesNEW.Rows(e.RowIndex).Cells(e.ColumnIndex).Value

    End Sub

    Private Sub DG_platesNEW_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DG_platesNEW.DataError

        If TypeOf e.Exception Is System.Data.NoNullAllowedException Then
            e.Cancel = True
            MessageBox.Show("Field must have a value!", "Please enter some valid data.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            DG_platesNEW.Rows(e.RowIndex).Cells(cellno).Value = mSavedValue
        ElseIf TypeOf e.Exception Is System.FormatException Then
            e.Cancel = True
            MessageBox.Show("Value not allowed!", "Please enter some valid data.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            DS_plates1.RejectChanges()
          ' DG_platesNEW.Rows(e.RowIndex).Cells(cellno).Value = mSavedValue
        End If

    End Sub

The part I am struggling to figure out is the FormatException error. I am trying to reject changes and re-insert the mSavedValue value back into the cell. However, when I test this, I need to run through the if statement twice for it to replace the value. However, once I have run through the error message twice, I can then use this error trap anywhere on that row and it works fine. Once I move from the row and test it on another row I get the same problem... Run it through the error twice though and again the value is replaced.

Any ideas guys?

Cheers
0
Comment
Question by:FMabey
2 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 1000 total points
ID: 19661481
Have a look at this little demo.  One form, two datagridviews - dgv1 and dgv2 - and this code.

Public Class Form1

    Private dt As New DataTable("TestTable")

    Private Sub filltable()
        For i As Integer = 0 To 2
            Dim col As New DataColumn("Col" & i.ToString, GetType(Integer))
            dt.Columns.Add(col)
        Next
        For i As Integer = 1 To 5
            Dim row As DataRow = dt.NewRow
            For j As Integer = 0 To 2
                row(j) = i * j
            Next
            dt.Rows.Add(row)
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        filltable()
        dgv1.DataSource = dt
        dgv2.DataSource = dt
    End Sub

    Private Sub dgv1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dgv1.DataError
        MsgBox("Error")
        e.Cancel = True
        dgv1.CancelEdit()
    End Sub
End Class

It is to demonstrate, albeit somewhat superficially, how changes are "committed" so far as bound datagridviews are concerned.  Make a (valid) change in a cell in dgv1 then move to a cell on the same row in that dgv.  You will notice that dgv2 doesn't change.  Now move to a cell on a different row in dgv1.  You will notice that dgv2 does change.  That doesn't actually _prove_ the relevant point, but it exemplifies it.  And it is that changes made in a datagridview are only (automatically) sent on - "committed" - to the (ulitmate) datasource when focus shifts to a different row/record.  So, until that has happened, they can be reversed in the dgv itself: there is no need to save the old value and put it in the datasource or to .RejectChanges on the datasource.

Now put an invalid entry in a cell in dgv1 and move either to a different cell on the same row, or to a different row in the same dgv, or even to the other dgv.  The movement itself is cancelled by e.Cancel = True and the wrong value will be replaced by the original right one by the .CancelEdit.

I suppose there may be reasons unconnected with the specific code that you show here why you need to take the steps that your code does show.  But I just wondered whether you were making unnecessary work for yourself rathr than letting the datagridview do its own thing (with just a little help from your code).

Roger
0
 
LVL 3

Author Comment

by:FMabey
ID: 19661736
Looks like I was making a mountain out of a mole hill! Still, I learnt a few things whilst doing so! Thanks!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

840 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