Telerik UI for ASP.NET AJAX

In order to validate the input in RadGrid when editing and inserting records, you could use a combination of the ValidationSettings properties exposed by the control and validation controls added to make the validation in RadGrid more flexible.

Validation settings

RadGrid exposes the ValidationSettings class to give you control over the validation on submit inside the control. The available properties are:

  • CommandsToValidate - An array of string used to list the names of the commands which will trigger validation in RadGrid.

  • EnableModelValidation - A boolean value idicating whether model validation should be enabled.

  • EnableValidation - A boolean value idicating whether the grid values should be validated upon submit.

  • ValidationGroup - A property used to set a ValidationGroup for the grid control, so that it is validated only along a certain group of controls having the same group name.

Adding a validator to an edit field in Telerik RadGrid

RadGrid provides an easy way to add required field validators to your generated input controls in Edit mode. This feature is applicable to:

  • GridBoundColumn

  • GridNumericColumn

  • GridHTMLEditorColumn

  • GridDropDownColumn

  • GridDateTimeColumn

The ColumnValidationSettings property is used to control validation of a given column editor via autogenerated RequiredFieldValidator and ModelBinding ModelErrorMessage control. This property is read-only, however, you can set the properties of the ColumnValidationSettings object it returns.

The properties can be set either declaratively or programmatically:

CopyASPX
 <telerik:GridBoundColumn DataField="ShipName" HeaderText="ShipName" UniqueName="ShipName">
    <ColumnValidationSettings EnableRequiredFieldValidation="true" EnableModelErrorMessageValidation="true">
        <RequiredFieldValidator ForeColor="Red" ErrorMessage="This field is required"></RequiredFieldValidator>
        <ModelErrorMessage BackColor="Red" />
    </ColumnValidationSettings>
</telerik:GridBoundColumn>

Note

ModelErrorMessage control validation is available only in .NET 4.5 when ModelBinding is used.

Adding a validator explicitly

In order to prevent the update operation if the user enters data in an edit field which is not in the correct format, you can wire the ItemCreated event of the grid to add a validator of your choice. There are four stages which need to be passed:

  1. Check whether the current item is in edit mode;

  2. Obtain reference to the respective GridColumnEditor instance and the editing control inside it;

  3. Create the validator and set its ControlToValidate property to point to the editing control;

  4. Add the validator to the Controls collection of the editing control Parent.

Below are the code snippets of an example in which we add RequiredFieldValidator to the TextBox editor of GridBoundColumn.

Note

With version older than Q1 2013 you should use the code snippet below to add RequiredFieldValidator to the TextBox editor of GridBoundColumn

Adding a validator to EditItemTemplate of GridTemplateColumn

The approach is the same as with the standard MS DataGrid control. You can place the respective validator in the EditItemTemplate of your GridTemplateColumn and relate its ControlToValidate property with the ID of the control you would like to validate. In the forthcoming sample the RequiredFieldValidator control will validate the TextBox control inside the EditItemTemplate:

CopyASPX
<telerik:RadGrid ID="RadGrid1" runat="server">
  <MasterTableView AutoGenerateColumns="False">
    <Columns>
      <telerik:GridTemplateColumn HeaderText="ContactName" UniqueName="TemplateColumn">
        <EditItemTemplate>
          <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ContactName") %>'></asp:TextBox>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
            ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
        </EditItemTemplate>
        <ItemTemplate>
          <asp:Label ID="Label1" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label>
        </ItemTemplate>
      </telerik:GridTemplateColumn>
      <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
      </telerik:GridEditCommandColumn>
    </Columns>
  </MasterTableView>
</telerik:RadGrid>

Server-side validation In some scenarios you may want to disable the client-side validation for the grid input and perform the whole validation process server-side.Here is a sample means to perform server-side validation using CustomValidator and its ServerValidate event (the validator error message will be displayed when the user enters string in the textbox with ID=TextBox1 which starts with X character):

Custom validation For more complex/custom validation operations you can extend the default ASP.NET validation and integrate CustomValidator control inside your grid instance. Here is an online resource which gives an example of operating with CustomValidators and configuring them to validate the user input as per your requirements:http://aspnet.4guysfromrolla.com/articles/073102-1.aspx