I created a support ticket and asked the following question:
I am binding a RadGridView to a SQL Server table.
The code below returns the following Data Exception Error:
Cannot set column 'SSAN'. The value violates the MaxLength limit of this column.
The SSAN field is a varchar(9). It should store 111223333 and not 111-22-3333. How can I set the column to save the text without the literals?
Dim col As GridViewMaskBoxColumn = New GridViewMaskBoxColumn
With col
.Name = "SSAN2"
.FieldName = "SSAN"
.HeaderText = "SSN"
.MaskType = MaskType.Standard
.Mask = "000-00-0000"
.TextAlignment = ContentAlignment.MiddleCenter
.DataType = GetType(String)
'.FormatString = "{0:000-00-0000}"
End With
RadGridView1.MasterTemplate.Columns.Add(col)
Svett from Telerik answered with the following:
You can achieve that by creating a custom editor:
Public
Class
CustomMaskedEditor
Inherits
RadMaskedEditBoxEditor
Public
Overrides
Property
Value()
As
Object
Get
Dim
value__1
As
Object
=
MyBase
.Value
value__1 = Convert.ToString(value__1).Replace(
"-"
,
String
.Empty)
Return
value__1
End
Get
Set
MyBase
.Value = value__1
End
Set
End
Property
End
Class
Then you should use the EditorRequired event of RadGridView to replace the default one:
Private
Sub
radGridView1_EditorRequired(sender
As
Object
, e
As
EditorRequiredEventArgs)
If
e.EditorType =
GetType
(RadMaskedEditBoxEditor)
Then
e.EditorType =
GetType
(CustomMaskedEditor)
End
If
End
Sub
I did not realize that my ticked had been closed because of my delayed response and I asked Svett if the following would not also be a solution:
Imports Telerik.WinControls.UI
Public Class CustomMaskedEditor
Inherits RadMaskedEditBoxEditor
Public Overrides Sub BeginEdit()
MyBase.MaskTextBox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
MyBase.BeginEdit()
Me.EditorElement.Focus()
End Sub
End Class
Can anyone tell me if this is not an acceptable way to only have the numbers saved to the database?