I have a DataGridView with one DataGridTextBoxColumn
. It is a bound to a datasource. The user can enter values such as -$12,345,678.90
(formatting is applied when tabbing out).
So what I am trying to achieve is when a user types a value like this: -$12,345,678.90
, in my DataGridTextBoxColumn
, I need to check the number's length, allowing a maximum length, which in my example is 10
(excluding - $ , .
).
If the input number reaches a length of 10
, I need to prevent the user from entering more numbers.
In the KeyDown
event handler, I tried to use Regular Expression to remove the - $ , .
symbols from whatever the user entered in the Cell, to find the length of the input and check whether it exceeded the maximum allowed length (e.g., 10
). Like this:
//length = length value returned after performing Regex replacement with empty
If length = 10 Then
Handled = True
End if
I also set my DataGridTextBoxColumn
max length to 15 (including $ - . ,
) while setting up the Grid.
The problem with this approach is that when a user tries to update a number, for example changing $12,345,678.90
to $22,345,678.90
, it will treat it as a new input, so it will fail and not allow the user to update a number.
I also tried some other events such as CellEndEdit
, CellValueChanged
but these events are only firing when I tab out or the focus is moved from the Cell.
How can I handle this?