ComponentOne FlexGrid for WinForms
Validation
Using the C1FlexGrid Control > Editing Cells > Validation

In many cases, edit masks alone are not enough to ensure that the data enters by the user was valid. For example, 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 these cases, 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.

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

To write code in Visual Basic

Visual Basic
Copy Code
Private Sub _flex_ValidateEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.ValidateEditEventArgs) Handles _flex.ValidateEdit
 
    ' Validate amounts.
    If _flex.Cols(e.Col).DataType Is GetType(Decimal) Then
        Try
            Dim dec As Decimal = Decimal.Parse(_flex.Editor.Text())
            If (dec < 1000) Or (dec > 10000) Then
                MessageBox.Show("Value must be between 1,000 and 10,000")
                e.Cancel = True
            End If
        Catch
            MessageBox.Show("Value not recognized as a Currency")
            e.Cancel = True
        End Try
    End If
End Sub

To write code in C#

C#
Copy Code
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;
        }
    }
}
See Also