DataGridTextBoxColumn

I'm inheriting from DataGridTextBoxColumn so that I can hook into the KeyPress and Validating events... problem, the Validating event doesn't fire if you tab out of the edit, only if you mouse out. Any ideas? (VS 2003)
[220 byte] By [TaDa] at [2007-12-24]
# 1
I don't recall any problems with the Validating event in VS 2003. Is it possible that the next control in the tab order just does not have "CausesValidation" set? (That is what defines whether or not the Validating event is generated...)
DeborahK at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Here's my code, the CurrencyValidate only fires when you leave with the mouse:

Public Class DataGridCurrencyColumn

Inherits System.Windows.Forms.DataGridTextBoxColumn

Dim TB As System.Windows.forms.TextBox

Public Sub New()

MyBase.New()

TB = Me.TextBox

TB.CausesValidation = True

AddHandler TB.KeyPress, AddressOf CurrencyKeyPress

AddHandler TB.Validating, AddressOf CurrencyValidating

End Sub

Protected Sub CurrencyValidating(ByVal Sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

Dim Val As Decimal

Try

Val = Decimal.Parse(TB.Text)

Catch ex As Exception

Val = 0

End Try

TB.Text = Microsoft.VisualBasic.Format(Val, "0.00")

End Sub

Protected Sub CurrencyKeyPress(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

Dim keyascii As Integer

keyascii = Asc(e.KeyChar)

Select Case keyascii

Case 45, 46, 48 To 57, 8, 13 'numbers, decimal, backspace, return, minus

Case Else

keyascii = 0

End Select

If keyascii = 0 Then

e.Handled = True

Else

e.Handled = False

End If

End Sub

End Class

TaDa at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Just to ensure we are clear ... CausesValidation is a very odd property. When you set CausesValidation to true for the text box, it means that when the text box gets focus that the PRIOR control that had focus will have its Validating event generated. So it does not matter if the text box you have has CausesValdation set - but rather if the NEXT control in the TAB order has it set.

To put this in context, this should be set to True for every control but the Cancel button (or similar controls). The idea is that if the user clicked on Cancel that the prior controls would NOT get validated because the user just cancelled.

Hope this helps...

DeborahK at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
I am clear on that and it isn't the issue... the Validating event DOES fire when you exit the control with the mouse, it doesn't fire when you TAB or ARROW out. The Leave event fires everytime, but by then it's too late, the datagrid has already updated the datasource.
TaDa at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
After further investigation, the Event is getting fired, but after the datagrid's Commit() has already written the data.
TaDa at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...