ComponentOne FlexGrid for WinForms
Step 4 of 6: Add Data Validation
FlexGrid for WinForms Tutorials > Edit Tutorial > Step 4 of 6: Add Data Validation

If you assign a data type to a grid column, the grid will ensure that only data of the proper type is stored in that column. This helps prevent errors, but you will often need stricter validation to ensure that the data entered is correct. For that, you should use the ValidateEdit event.

For example, imagine that anti-trust regulations prevent us from selling our Applets in the North region. To prevent data-entry mistakes and costly lawsuits, we want to prevent users from entering this combination into the control.

The following event checks the data after each edit and prevents invalid entries. Add the following code to the form:

To write code in Visual Basic

Visual Basic
Copy Code
Private Sub C1FlexGrid1_ValidateEdit(ByVal sender As Object, ByVal e As ValidateEditEventArgs) Handles C1FlexGrid1.ValidateEdit
    Dim rgn As String = String.Empty
    Dim prd As String = String.Empty

     ' Collect the data we need.
    Select Case e.Col
        Case 0
            prd = C1FlexGrid1.Editor.Text
            rgn = C1FlexGrid1(e.Row, "Region")
        Case 1
            prd = C1FlexGrid1(e.Row, "Product")
            rgn = C1FlexGrid1.Editor.Text
    End Select

     ' We can't sell Applets in the North Region.>
    If prd = "Applets" And rgn = "North" Then
        MsgBox("Warning: Regulation #12333AS/SDA-23 forbids " & _
               "the sale of " & prd & " in region " & rgn & ". " & _
               "Please verify input.")
        e.Cancel = True
    End If
End Sub

To write code in C#

C#
Copy Code
private void c1FlexGrid1_ValidateEdit( object sender, ValidateEditEventArgs e)
{
     string rgn = string.Empty;
     string prd = string.Empty;

     // Collect the data we need.
    switch (e.Col)
    {
        case 0:
            prd = c1FlexGrid1.Editor.Text;
            rgn = (string)c1FlexGrid1[e.Row, "Region"];
            break;
        case 1:
            prd = (string)c1FlexGrid1[e.Row, "Product"];
            rgn = c1FlexGrid1.Editor.Text;
            break;
    }

     // We can't sell Applets in the North Region.
    if ( prd == "Applets" && rgn == "North" )
 {
        MessageBox.Show("Warning: Regulation #12333AS/SDA-23 forbids " +
               "the sale of " + prd + " in region " + rgn + ". " +
               "Please verify input.");
        e.Cancel = true;
    }
}

Run the program and observe the following:

The function starts by gathering the input that needs to be validated. Note that the values being checked are retrieved using the Editor.Text property. This is necessary because the edits have not yet been applied to the control. If the test fails, the function displays a warning and then sets the Cancel parameter to True, which cancels the edits and puts the cell back in edit mode so the user can try again.

Press F5 to run the project again, then try inputting some bad values. You will see that the control will reject them.