This is a migrated thread and some comments may be shown as answers.

MaskFormat.ExcludePromptAndLiterals

4 Answers 243 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 31 Aug 2012, 05:53 PM

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? 

4 Answers, 1 is accepted

Sort by
0
Don
Top achievements
Rank 1
answered on 31 Aug 2012, 09:33 PM
I would do something like this, which is almost the same as what you had come up with:
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)
    .TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
End With
RadGridView1.MasterTemplate.Columns.Add(col)
0
Peter
Telerik team
answered on 04 Sep 2012, 10:28 AM
Hello,

Thank you both for writing.
@John - you should use the solution proposed by Don - let us know if this does not help for your scenario.
@Don - I have updated your Telerik points for the community effort.

Greetings,

Peter
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
John
Top achievements
Rank 1
answered on 04 Sep 2012, 02:31 PM
Don & Peter,

Thank you for your responses. I had already tried this. When I attempt to set col.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals I recieve 'TextMaskFormat' is not a member of 'Telerik.WinControls.UI.GridViewMaskBoxColumn'. What am I missing?

Thanks again for your help.

0
Peter
Telerik team
answered on 05 Sep 2012, 12:51 PM
Hi John,

I am sorry for the caused inconvenience. We will introduce the GridViewMaskBoxColumn's TextMaskFormat property in our next release. For the time being, you should use the Svet's 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

Do not hesitate to contact us if you have other questions.

Regards,

Peter
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Don
Top achievements
Rank 1
Peter
Telerik team
John
Top achievements
Rank 1
Share this question
or