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

RadGrid Boundcolumn with ColumnValidator to allow only integers

1 Answer 36 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alexander
Top achievements
Rank 1
Alexander asked on 27 Aug 2014, 07:07 PM
I have a boundcolumn with a column validator that looks like this:

<telerik:GridBoundColumn DataField="January" HeaderText="Jan" Aggregate="Sum" FooterAggregateFormatString="{0:n0}">
                            <ColumnValidationSettings EnableRequiredFieldValidation="true">
                                <RequiredFieldValidator ForeColor="Red" ErrorMessage="January required" Text="*" Display="Dynamic"></RequiredFieldValidator>
                            </ColumnValidationSettings>
                        </telerik:GridBoundColumn>


The field January is an integer. How would I be able to make it allow ONLY integers. Currently I can enter "hello world" and save it.

1 Answer, 1 is accepted

Sort by
0
Alexander
Top achievements
Rank 1
answered on 27 Aug 2014, 07:54 PM
After some looking around, I decided to do the validation in the VB.NET instead of the ASP.

I removed the <columnvalidationSettings>

and in my ItemCreated I added:

If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
                Dim item As GridEditableItem = CType(e.Item, GridEditableItem)
                Dim editor As GridTextBoxColumnEditor = CType(item.EditManager.GetColumnEditor("January"), GridTextBoxColumnEditor)
                Dim cell As TableCell = CType(editor.TextBoxControl.Parent, TableCell)
                Dim validator As RequiredFieldValidator = New RequiredFieldValidator
                Dim secondValidator As CompareValidator = New CompareValidator
                secondValidator.ControlToValidate = editor.TextBoxControl.ID
                secondValidator.Type = ValidationDataType.Integer
                secondValidator.Operator = ValidationCompareOperator.DataTypeCheck
                secondValidator.Text = "*"
                secondValidator.ErrorMessage = "January must be an integer"
                cell.Controls.Add(secondValidator)
                validator.ControlToValidate = editor.TextBoxControl.ID
                validator.Text = "*"
                validator.ErrorMessage = "January is Required"
                cell.Controls.Add(validator)
End If

This way I added the validator for it to not be empty and the validator for it to not be anything other than an integer.
Tags
Grid
Asked by
Alexander
Top achievements
Rank 1
Answers by
Alexander
Top achievements
Rank 1
Share this question
or