Skip to content

DevExpress-Examples/asp-net-web-forms-grid-implement-custom-date-validation-in-batch-edit-mode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Grid View for ASP.NET Web Forms - How to implement custom date validation in batch edit mode

This example demonstrates how to validate date column values on the client and server when the grid is in batch edit mode.

dateValidation

Overview

Follow the steps below:

  1. On the client side, handle the grid's BatchEditRowValidating event. In a handler, use the validationInfo argument property to get information about the modified row and call the CheckYears function to calculate the difference between the values of particular cells. Based on the calculated number, define whether the entered data is valid and specify an error text string for invalid data cells.

    <dx:ASPxGridView ID="ASPxGridView1" ClientInstanceName="grid" runat="server" KeyFieldName="EmployeeID">
        <SettingsEditing Mode="Batch" />
        <!-- ... -->
        <ClientSideEvents BatchEditRowValidating="OnValidation" />
    </dx:ASPxGridView>
    function OnValidation(s, e) {
        // ...
        var grid = ASPxClientGridView.Cast(s);
        var cellInfo1 = e.validationInfo[grid.GetColumnByField("BirthDate").index];
        var cellInfo2 = e.validationInfo[grid.GetColumnByField("HireDate").index];
        var years = CheckYears(cellInfo1.value, cellInfo2.value);
        if (years == null || years < 18) {
            cellInfo1.isValid = false;
            cellInfo2.isValid = false;
            cellInfo2.errorText = "Invalid difference between Hire Date and Birth Date";
            cellInfo1.errorText = "Invalid difference between Hire Date and Birth Date";
        } else {
            cellInfo1.isValid = true;
            cellInfo2.isValid = true;
        }
    }
    function CheckYears(date1, date2) {
        if (!date1 || !date2)
            return null;
        var msecPerYear = 1000 * 60 * 60 * 24 * 365;
        var years = (date2.getTime() - date1.getTime()) / msecPerYear;
        return years;
    }
  2. On the server side, handle the grid's RowValidating event. In a handler, use the NewValues argument property to get the new values of modified cells and call the CheckYears function to get the result of date calculation. Use the Errors and RowError argument properties to specify error text strings for invalid data cells and the Error Row.

    <dx:ASPxGridView ID="ASPxGridView1" runat="server" KeyFieldName="EmployeeID"
        OnRowValidating="ASPxGridView1_RowValidating" ...>
        <SettingsEditing Mode="Batch" />
        <!-- ... -->
    </dx:ASPxGridView>
    private bool CheckYears(DateTime hireDate, DateTime birthDate) {
        TimeSpan span = hireDate - birthDate;
        if (span.TotalDays / 365 < 18)
            return false;
        else
            return true;
    }
    
    void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText) {
        if (errors.ContainsKey(column)) return;
        errors[column] = errorText;
    }
    
    protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e) {
        DateTime birthDate = (DateTime)e.NewValues["BirthDate"];
        DateTime hireDate = (DateTime)e.NewValues["HireDate"];
        var result = CheckYears(hireDate, birthDate);
        if (!result) {
            AddError(e.Errors, ASPxGridView1.Columns["BirthDate"], "Invalid difference between Hire Date and Birth Date");
            AddError(e.Errors, ASPxGridView1.Columns["HireDate"], "Invalid difference between Hire Date and Birth Date");
            e.RowError = "Correct validation errors";
        }
        // ...
    }
  3. Use the grid's AllowValidationOnEndEdit property to specify whether to validate data when a cell switches from edit to browse mode or on the Save changes button click.

    <dx:ASPxCheckBox runat="server" ID="ASPxCheckBox2" AutoPostBack="true" Checked="false"
        Text="Validate on the Save Changes button click" OnCheckedChanged="ASPxCheckBox2_CheckedChanged" />
    protected void ASPxCheckBox2_CheckedChanged(object sender, EventArgs e) {
        ASPxGridView1.SettingsEditing.BatchEditSettings.AllowValidationOnEndEdit = !ASPxCheckBox2.Checked;
    }

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Validate date column values on the client and server when the grid is in batch edit mode.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •