Working with C1FlexSheet / Sheet Operations / Data Validation
Data Validation

There are a lot of ways to validate data in FlexSheet. We will explain a simple way of validation, which is throwing an exception on passing an invalid value.

To validate data, we have created a class named ProductBase which contains data about product line, color, name, price, cost, weight, volume discontinued, and rating. Another class named ProductRow contains the SetValue method which simply throws exceptions when a property setter is passed an invalid value. This method allows for property-level validation only (no item-level validation). The following code implements data validation in SetValue method:

    Try
        If p = "Price" AndAlso DirectCast(value, System.Nullable(Of Double)) <= 0 Then
            Throw New Exception("Price must be > 0.")
        End If
        If p = "Cost" AndAlso DirectCast(value, System.Nullable(Of Double)) <= 0 Then
            Throw New Exception("Cost must be > 0.")
        End If
        If p = "Weight" AndAlso DirectCast(value, System.Nullable(Of Double)) <= 0 Then
            Throw New Exception("Weight must be > 0.")
        End If
        If p = "Rating" AndAlso (DirectCast(value, System.Nullable(Of Integer)) < 0 _
               OrElse DirectCast(value, System.Nullable(Of Integer)) > 5) Then
            Throw New Exception("Rating must be between 0 and 5.")
        End If
        MyBase.SetValue(p, value)
    Catch e As Exception
        MessageBox.Show(e.Message)
    End Try
End Sub

The ProductThrow class throws exception when a user sets price, cost or weight properties to a negative value or sets rating to a value less than zero and greater than 5.