In any business application, validating user input is a necessary, compulsory task. For this reason, ComponentOne FlexGrid for WinForms offers two techniques for enhanced cell-level data validation.

Validation Using Data Annotations

Data Annotations are attributes applied to the class or class members that specify validation rules, how the data is displayed, and set relationships between classes.

The System.ComponentModel.DataAnnotations namespace contains the classes used as data attributes. Applying these attributes to the data class or member centralizes the data definition, eliminating the need to re-apply the same rules in multiple places.

C1FlexGrid v1 defines the attributes of data annotations while creating data objects via code. This allows C1Flexgrid to support Data Annotations that validate user input for each cell.

The grid recognizes these attributes and automatically applies the rules to each of its columns.

Here are the annotation attributes supported in C1FlexGrid:

Attribute Name Functionality in FlexGrid
Association Specifies that a member of an entity represents a data relationship, such as a foreign key relationship
Display Provides a general-purpose attribute that allows you to specify localizable strings for types and members of entity partial classes
DisplayFormat Specifies how data fields are displayed and formatted
DisplayColumn Specifies the column that is displayed within the referred table as a foreign-key column
Editable Indicates whether or not a data field can be edited
Key Denotes one or more properties that identify an entity uniquely
Validation RequiredAttribute StringLengthAttribute RangeAttribute RegularExpressionAttribute MinLengthAttribute MetaDataAttribute MaxLengthAttribute EmailAddressAttribute CompareAttribute DataTypeAttribute The data annotation validation attributes are used as validation rules in FlexGrid operations.

Let’s consider an example.

A list of employees for an organization includes common fields like name, email, age, and contact. Every field has a set of criteria to pass, and data annotation validates the input.

Data Validation in FlexGrid for WinForms

Implementing Data Annotations in the Grid

To implement data annotation for the above example, create a class named EmployeeData that contains the following properties for each employee.

class EmployeeData
{
public string EmployeeName {get; set;} public UInt32 Age {get; set;}
public string EmployeeEmail {get; set;} public string Contact {get; set;}
}

With the class created, follow these steps:

  1. Add metadata tags for each property to apply validation
  2. Apply data annotation attributes to the EmployeeName, Age, and EmployeeEmail properties
  3. Change the display name of the EmployeeName property to “Name”, which is reflected as a caption of the grid’s column
  4. Set format for the Age so it shows “yrs.” as a suffix after the corresponding value
  5. Change the display name of the EmployeeEmail property to “Email” and validate that it is a required property and cannot be left empty
[Display (Name = "Name")]
public string EmployeeName {get; set;}

[DisplayFormat (DataFormatString = "0 yrs.")] public UInt32 Age {get; set;}

[Display (Name = "Email")]
[Required (Error Message = "Email ID is Required")] public string EmployeeEmail {get; set;}

After defining the attributes of these properties in the class, create a list of EmployeeData objects. Set this list as the DataSource of the grid to show the records.

Data Validation in FlexGrid for WinForms

Now, the input is validated while creating the data object via code. If the data source does not have to be set through the class object, FlexGrid offers a new property called EditorValidation.

Validation Using the EditorValidation Property

C1FlexGrid v1 includes a column-level property called EditorValidation. This property consists of validation rules that enhance data evaluation when applied to the columns of the grid.

When annotations are used, they are translated to the EditorValidation property. When they are not used, validations in the grid can be applied to columns using their EditorValidation property.

Below we apply the previously discussed rules using the EditorValidation property and validate two columns.

For the EmployeeName column, we will apply the following rules:

  1. RequiredRule: The field cannot be empty
  2. StringLengthRule: Minimum length for the input is set to a specific value

Similarly, we will validate the Age column using the RangeRule.

var Name = c1FlexGrid1.Cols["EmployeeName"]; Name.EditorValidation.Add(new RequiredRule()); Name.EditorValidation.Add(new StringLengthRule()
{
MinimumLength = 5
});

var Age = c1FlexGrid1.Cols["Age"]; Age.EditorValidation.Add(new RequiredRule()); Age.EditorValidation. Add(new RangeRule()
{
Minimum = 18,
Maximum = 50
});

Data Validation in FlexGrid for WinForms

Here, we detailed validating data in the FlexGrid control. A sample is attached here. Please feel free to try it out and leave your feedback or questions in the comments section.

Try ComponentOne Controls

Download the latest version of ComponentOne Studio for Xamarin

Download Now!