Validation

FlexGrid helps with validation in several ways:

  1. Automatic Type Coercion: When cell edits are committed, the grid automatically coerces values to match the column's data type. It also checks for nulls and empty values based on the column's isRequired property. If the data is invalid, the original cell value is restored.
  2. Data Maps: If your grids contain columns that should only allow certain input values, add a DataMap to the column to ensure users will not be able to type invalid content into the cells.
  3. Event-Based Validation: The grid raises a cellEditEnding event that allows you to check the editor's current value and cancels the edit if the new value is invalid.
  4. CollectionView-Based Validation: The grid honors the collection view's getError method to highlight invalid cells and to prevent users from entering invalid information.

The grid below demonstrates the first three ways of validation. It has a Country column with a data map that prevents users from entering invalid countries. The cellEditEnding event is used to ensure that the 'Sales' and 'Expenses' columns contain positive values:

ID
Country
Sales
Expenses
Overdue
0
US
81,732.54
38,401.13
1
Germany
20,603.32
27,944.24
2
UK
44,217.79
48,877.49
3
Japan
29,190.63
23,365.74
4
Sweden
46,951.19
49,107.56
5
Norway
86,237.02
49,767.35
6
Denmark
31,459.18
40,845.40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// This file locates: "Scripts/Lesson/C1FlexGrid/Validation.js".
c1.documentReady(function () {
    var theGrid = wijmo.Control.getControl('#theGrid');
    var countries = 'US,Germany,UK,Japan,Sweden,Norway,Denmark'.split(',');
 
    theGrid.columns[1].dataMap = countries;
    theGrid.cellEditEnding.addHandler(function(s, e) {
        var col = s.columns[e.col];
        if (col.binding == 'Sales' || col.binding == 'Expenses') {
            var value = wijmo.changeType(s.activeEditor.value, wijmo.DataType.Number, col.format);
            if (!wijmo.isNumber(value) || value < 0) {
                e.cancel = true;
                alert('Please enter a positive amount.')
            }
        }
    });
});
1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Web.Mvc;
 
namespace LearnMvcClient.Controllers
{
    public partial class C1FlexGridController : Controller
    {
        // GET: Validation
        public ActionResult Validation()
        {
            return View(Models.FlexGridData.GetSales(Models.FlexGridData.Countries7));
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@model IEnumerable<FlexGridData.Sale>
 
<h1>
    @Html.Raw(Resources.C1FlexGrid.Validation_Title)
</h1>
<p>
    @Html.Raw(Resources.C1FlexGrid.Validation_Text1)
</p>
<ol>
    <li>
        @Html.Raw(Resources.C1FlexGrid.Validation_Text2)
    </li>
    <li>
        @Html.Raw(Resources.C1FlexGrid.Validation_Text3)
    </li>
    <li>
        @Html.Raw(Resources.C1FlexGrid.Validation_Text4)
    </li>
    <li>
        @Html.Raw(Resources.C1FlexGrid.Validation_Text5)
    </li>
</ol>
 
<p>
    @Html.Raw(Resources.C1FlexGrid.Validation_Text6)
</p>
@(Html.C1().FlexGrid().Id("theGrid").Height(200)
    .AutoGenerateColumns(false)
    .Bind(Model)
    .Columns(cs=>
    {
        cs.Add().Binding("Id").Header("ID").Width("50").IsReadOnly(true);
        cs.Add().Binding("Country").Header("Country").IsRequired(true);
        cs.Add().Binding("Sales").Header("Sales").Format("n2");
        cs.Add().Binding("Expenses").Header("Expenses").Format("n2");
        cs.Add().Binding("Overdue").Header("Overdue");
    })
)