PFGrid toolkit and controls for .NET Winforms

...Empower your UI with our fast and flexible controls!

Data-Validation for Cells in Winforms PFGrid .NET

In this tutorial is described how to use validation of user-input in PFGrid.NET. If cell-data is not valid, the cell will be displayed with a red error-icon and a tooltip that shows the error-text.

 

The sample

In our sample we display the content of the table Employees of the NorthWind-database. The fields Lastname is editable but if the text is empty or the length exceeds 15 letters, an error will be displayed:

Img. 1: Display error for a cell

How to validate

Validating user-input in PFGrid.NET is pretty simple: Bind the event BeforeCellValueChange and set the Error-text if the input is not valid:

 

private void treeListView_BeforeCellValueChange(object sender,

    Controls.Grid.Events.CellCancelEventArgs e)

{

    if (e.NewValue.ToString() == String.Empty)

    {

        e.Error = "No empty values for Lastname allowed";

    }

    else if (e.NewValue.ToString().Length > 15)

    {

        e.Error = "Maximum length of Lastname is 15 letters";

    }

}

 

Appearance of the Error-Icon

The grid has a property ErrorProviderCells. This can be used to change the error-icon, the padding or blink-rate:

 

private void treeListView_BeforeCellValueChange(object sender,

    Controls.Grid.Events.CellCancelEventArgs e)

{

    // Set padding of the error icon

    treeListView.ErrorProviderCells.SetIconPadding(

        treeListView.GetCurrentEditor() as Control, -40);

 

    // Set the error-icon

    treeListView.ErrorProviderCells.Icon = Resources.Messages;