Question DGV CellFormatting and CellValidating issues

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
On as DataGridView I have two events

VB.NET:
Private Sub InvoiceMonthlyDGV_ChangeEvents(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs)
        If ADG1.Columns(e.ColumnIndex).Name = "Amount" Then
            If e.FormattedValue IsNot Nothing Then
                Dim Result As Boolean
                Dim isValid As Boolean = Double.TryParse(e.FormattedValue.ToString(), Result)
                If isValid = False Then
                    ADG1.Rows(e.RowIndex).ErrorText = e.FormattedValue.ToString & " is not a valid amount!"
                    e.Cancel = True
                Else
                    ADG1.Rows(e.RowIndex).ErrorText = Nothing
                End If
            End If
        End If
    End Sub

VB.NET:
Private Sub InvoiceMonthlyFormatCells(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs)
        If ADG1.Columns(e.ColumnIndex).Name = "Amount" Then
            e.CellStyle.BackColor = Color.Yellow
        End If
    End Sub

VB.NET:
AddHandler ADG1.CellFormatting, AddressOf InvoiceMonthlyFormatCells
AddHandler ADG1.CellValidating, AddressOf InvoiceMonthlyDGV_ChangeEvents

When the Form loads the CellFormatting event works fine, but somehow causes chaos with the CellValidating event . Remove the CellFormatting handler and CellValidating works fine.

Any ideas?

Thanks
 
If only we knew what "causes chaos" actually meant

Freezes the app! Each work fine separately, but run both together and input an invalid amount into the grid and that's the end of the show folks!
 
Have you debugged? There are only really two reasons that an app would freeze: a method never returns or you enter an infinite loop. You need to debug your code and work out exactly what path execution takes and where exactly things go wrong. Could it be that the two event handlers each cause the other event to be raised, so you end up in a tennis match bouncing back and forth between the two? These are the sorts of things that you can discover using the debugger.
 
VB.NET:
Have you debugged?

This is a windows app that depends upon a WebService running In debug can't get past the login form! No idea why, just got around to living with it!

Now the freezing has stopped, but .ErrorText doesn't display - it means the user can't leave the cell whilst there is an invalid value but, of course, they won't know why....

Spent nearly all day trying to pinpoint an error between the client and the server - turned out to be quite simple to fix, but a nightmare to find! Got a feeling this could be just as much fun!

Was just hoping that there might be a known conflict between CellFormatting and CellValidating.... Ho hum!
 
OK - I built a quick test app with VS2010 and managed to reproduce the error - debugging showed the following:-

VB.NET:
Private Sub InvoiceMonthlyFormatCells(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs)
        [COLOR="Red"]If ADG1.Columns(e.ColumnIndex).Name = "Amount" Then[/COLOR]            
e.CellStyle.BackColor = Color.Yellow
        End If
    End Sub

ArgumentOutOfRangeException was unhandled by user code

MMMmm - Still not really a lot wiser!
 
The solution, it would appear, is to add

VB.NET:
If Not e.ColumnIndex = -1 then

'Carry out the formatting

Runs OK on the test rig and the cell formatting remains unchanged! All is good...
 
Back
Top