We're trying to implement regular expression masking in the GridView the same way it's implemented elsewhere in the application where Telerik controls are used. Usually this means handling the ValueChanging event and comparing the current text to the RegEx, then setting e.IsHandled to reflect the validity of the current character. Is this possible to do on a per-column basis in the GridView? I understand the GridView has a KeyDown event that can be captured, but this isn't prefereable for maintainance reasons - it would require a huge amount of code in that handler. Is it possible to get KeyDown or Validating events on a per column basis?
Thanks!
7 Answers, 1 is accepted
You may handle the CellValidating event of the grid and verify that the validated cell corresponds to the column you want:
private void clubsGrid_CellValidating(object sender, GridViewCellValidatingEventArgs e) { //First option if (e.Cell.Column.UniqueName.Equals("MyUniqueName")) { } //Second option if((e.Cell.Column as GridViewDataColumn).DataMemberBinding.Path.Path == "MyProperty") { } }Depending on your particular scenario, you may try to implement a GridViewExpressionColumn.
Best wishes,
Maya
the Telerik team
I tried using the CellValidating event and it doesn't seem to fire at the same times as the Validating event on the masked edit control. With the mask control, the validating event fires at every keystroke. That's a good thing for our use as it can be used to prevent unwanted keypresses. On the other hand, the CellValidating event on the grid only fires when the cell receives or loses focus. Is there another event I could use to get notified any time the user makes an edit? I'd prefer not to use the KeyDown event on the grid because the code would need to filter out irrelevant keypresses such as the tab key and arrow keys.
Thanks for pointing out the Expression column. While the column looks useful for calcualted values it doesn't seem to fit the scenario of preventing incorrect data entry.
I might also take this opportunity to urge Telerik to please create proper regular expressions in the mask controls. Users could certainly appreciate the extra flexibility of a regex.
Thanks,
James
In this case you may define the CellEditTemplate of the column and set it as RadMaskedTextBox. Thus you will be able to subscribe directly to the ValueChanging event and handle it in the code behind.
As far as your requirements for the proper regular expressions for the mask controls, I would need a bit more details. May you specify what are the scenarios you want to cover and how do you want this to be implemented and used ?
Maya
the Telerik team
http://demos.devexpress.com/DXEditorsSilverlightDemos/
The email text entry is a good example - it's a mask control with a regular expression to filter out invalid characters in an email address. During editing the control both filters out invalid characters and shows the user that the eventual format should look like "_____@___.___". It would be great if Telerik had something similar for use both inside and outside of the grid. A search of these forums for "regular expressions" shows mentions of Telerik developing a regex control in 2009, unfortunately it doesn't look like that work was completed.
Thanks,
James
Currently, we do not have a built-in Regex expression validation for the RadMaskedTextBox. However, you can apply a regex expression on the MaskedText of the RadMaskedTextBox. You can use its ValueChanging event and check if the MaskedText satisfies the Regex. If it does not, you can handle the ValueChanging event (e.Handled = true) and revert to the old value. For Q1 2011, we will support the Data Annotation attributes, which provide Regex validation, with the new suite of masked input controls and you will be able to use that as well.
Greetings,Alex Fidanov
the Telerik team
In order to be able to reproduce the issue you reported and provide a suitable solution, I would need a bit more information. Is a value different when in edit and when in view mode ? It would be great if you could describe in further details the case and if possible to share any relevant code-snippets.
Maya
the Telerik team