Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

DataGridView formatting breaks validation

I've got a bound DataGridView that performs some validation exactly as I want it to - providing I don't have the columns formatted.

A code snippet example of the validation that works when I don't format the columns is:

If CType(e.ProposedValue, Integer) <= intPreviousToValue Then
e.Row.RowError = "Error message"
e.Row.SetColumnError(e.Column, "Error message")
End If

Using the standard VB.NET 2005 Express formatting properties, the columns are formatted to be:

Column 0 - Numeric - no decimals
Column 1 - Numeric - no decimals
Column 2 - Currency - two decimals

If I take the formatting out, the grid validation works fine - if I put the formatting back in, I can't get the validation to work.

What am I breaking by putting the formatting in?
[851 byte] By [jellyfish5000] at [2007-11-11 10:21:47]
# 1 Re: DataGridView formatting breaks validation
How does it "not work" with formatting enabled? Do you get an error message? If so, what does it say? Or, if the comparisons simply don't work as you expect them to, what are the values of e.ProposedValue and intPreviousToValue when that happens?
Phil Weber at 2007-11-11 20:48:07 >
# 2 Re: DataGridView formatting breaks validation
Really sorry for the delay in responding. I've only just had chance to get around looking at this again.
I am seeing that the e.ProposedValue and intPreviousToValue ARE being validated (for example, I can put a msgbox in there to prove its getting into the If .. End If).
What I'm not seeing is the flashing red exclamation mark that I had previously seen without the DataGridView formatting.
jellyfish5000 at 2007-11-11 20:49:07 >
# 3 Re: DataGridView formatting breaks validation
What are the values of e.ProposedValue and intPreviousToValue with validation enabled? What are the values when validation is disabled?
Phil Weber at 2007-11-11 20:50:06 >
# 4 Re: DataGridView formatting breaks validation
Hello Phil. Thanks for getting back.
I'm not sure I totally understand your question. Perhaps if I explained it further it may help?

The current grid has the following test values:
Col 0 Header = From
Col 1 Header = To
Col 2 Header = Price
Row 0 Col 0 = 1
Row 0 Col 1 = 9
Row 0 Col 2 = 5.00
Row 1 Col 0 = 10
Row 1 Col 1 = 19
Row 1 Col 2 = 4.50

If I change Row 1 Col 0 to, for example, 6, a breakpoint in the code reveals that the PreviousFromValue is 1, the PreviousToValue is 9, and the proposed value is 6, and so If ... End if checks are invoked as shown in the original code extract.

Its only now that I've got the grid formatted so that col 0 and 1 are numeric and col 2 is currency that I don't see the error exclamation mark. If I remove all the formatting, I get my error indicator back.

I guess I could simply put something on a status control or something to warn the user of an error, and re-set the focus back to the offending cell - which I may do anyway - but I'd still like to see the error exclamation marks flashing as was originally happening.
jellyfish5000 at 2007-11-11 20:51:11 >
# 5 Re: DataGridView formatting breaks validation
OK, so with formatting disabled, a breakpoint in the code reveals that the PreviousFromValue is 1, the PreviousToValue is 9, and the ProposedValue is 6, correct?

With formatting enabled, what are the values of PreviousFromValue, PreviousToValue and ProposedValue? What I'm getting at is that maybe the value includes formatting and is no longer a numeric value, which would throw off the validation.
Phil Weber at 2007-11-11 20:52:11 >