ComponentOne FlexGrid for WinForms
Editing Cells / Validation
In This Topic
    Validation
    In This Topic

    Using EditorValidation Property

    In many cases, edit masks and data annotations are not enough to ensure that the data entered by the user is valid. In such cases, you can use EditorValidation property of the Column class. The property consists of validation rules that can be applied to the columns of FlexGrid for enhanced data evaluation.

    The GIF below demonstrates StringLength and Required validation rules applied to the Customer column.

    A gif image showing validation in action.

    The below code example implements the EditorValidation property in the FlexGrid.

    private void SetupGridColumns()
    {
      var customerNameColumn = _flex.Cols["CustomerName"];
      customerNameColumn.Caption = "Customer";
      customerNameColumn.EditorValidation.Add(new RequiredRule());
      customerNameColumn.EditorValidation.Add(new StringLengthRule()
      {
        MinimumLength = 2
      });
      var customerIDColumn = _flex.Cols["CustomerID"];
      customerIDColumn.Visible = false;
      var frequencyColumn = _flex.Cols["Frequency"];
      frequencyColumn.Format = "0%";
      frequencyColumn.AllowEditing = false;
      var ageColumn = _flex.Cols["Age"];
      ageColumn.EditorValidation.Add(new RequiredRule());
      ageColumn.EditorValidation.Add(new RangeRule()
      {
        Minimum = 10,
        Maximum = 90
      });
    }
    

    Using ValidateEdit Event

    A mask won't let you specify a range of possible values, or validate the current cell based on the contents of another cell. In that case, to apply validation to the FlexGrid, you can trap the ValidateEdit event and see if the value contained in the Editor.Text property is a valid entry for the current cell (at this point, the cell still has the original value in it). If the input is invalid, set the Cancel parameter to True and the grid will remain in edit mode until the user types a valid entry.

    The code example below validates input into a currency column to ensure that values entered are between 1,000 and 10,000.

    private void _flex_ValidateEdit( object sender, ValidateEditEventArgs e)
    {
     
        // Validate amounts.
        if (_flex.Cols[e.Col].DataType == typeof(Decimal))
        {
            try
            {
                Decimal dec = Decimal.Parse(_flex.Editor.Text);
                if ( dec < 1000 || dec > 10000 )
                {
                    MessageBox.Show("Value must be between 1,000 and 10,000");
                    e.Cancel = true;
                }
            }
            catch
            {
                MessageBox.Show("Value not recognized as a Currency");
                e.Cancel = true;
            }
        }
    }
    
    Note: WinForms .NET 5 Edition has only runtime assemblies. Due to the new design-time model in VS2019 Preview, which is not complete yet from the Microsoft side, we do not supply any special design-time features as of yet. However, some of the controls might show up at design-time and allow editing few properties in the property grid.
    See Also