Spread WPF Documentation
Using Data Validation
Spread WPF Documentation > Developer's Guide > Managing the User Interface > Using Data Validation

You can create validators to validate the user data.

You can display a list of valid values for the user and display an invalid data image if the user types invalid data.

The default invalid image is a red ellipse.

You can use any of several types of validator methods to create the validation criteria.

Set the HighlightInvalidData property to use the red ellipse as an invalid data image. You can also customize the invalid data image with the InvalidDataPresenter control.

Using Code

The following code uses different types of validators to validate the data in B1, B2, and B3.

CS
Copy Code
gcSpreadSheet1.HighlightInvalidData = true;
var valid = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateListValidator("5,10,15,20");
gcSpreadSheet1.Sheets[0].Cells[0, 1].DataValidator = valid;
gcSpreadSheet1.Sheets[0].Cells[0, 0].Text = "5, 10, 15, and 20 are valid numbers.";
            
var valid1 = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateNumberValidator(GrapeCity.Windows.SpreadSheet.Data.ComparisonOperator.GreaterThan, "5", "20", true);
gcSpreadSheet1.Sheets[0].Cells[1, 1].DataValidator = valid1;
gcSpreadSheet1.Sheets[0].Cells[1,0].Text = "A number greater than 5 is valid.";

var valid2 = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateTextLengthValidator(GrapeCity.Windows.SpreadSheet.Data.ComparisonOperator.GreaterThan, "4", "20");
gcSpreadSheet1.Sheets[0].Cells[2, 1].DataValidator = valid2;
gcSpreadSheet1.Sheets[0].Cells[2, 0].Text = "Type more than four characters.";
VB.NET
Copy Code
GcSpreadSheet1.HighlightInvalidData = True
Dim valid = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateListValidator("5,10,15,20")
GcSpreadSheet1.Sheets(0).Cells(0, 1).DataValidator = valid
GcSpreadSheet1.Sheets(0).Cells(0, 0).Text = "5, 10, 15, and 20 are valid numbers."

Dim valid1 = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateNumberValidator(GrapeCity.Windows.SpreadSheet.Data.ComparisonOperator.GreaterThan, "5", "20", True)
GcSpreadSheet1.Sheets(0).Cells(1, 1).DataValidator = valid1
GcSpreadSheet1.Sheets(0).Cells(1, 0).Text = "A number greater than 5 is valid."

Dim valid2 = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateTextLengthValidator(GrapeCity.Windows.SpreadSheet.Data.ComparisonOperator.GreaterThan, "4", "20")
GcSpreadSheet1.Sheets(0).Cells(2, 1).DataValidator = valid2
GcSpreadSheet1.Sheets(0).Cells(2, 0).Text = "Type more than four characters."

Using Code

The following code customizes the invalid data image.

<Window.Resources>
        <Style TargetType="ss:InvalidDataPresenter">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ss:InvalidDataPresenter">
                        <Grid Background="{TemplateBinding Background}">
                            <Ellipse Stroke="Orange"
                                 StrokeThickness="2"
                                 Margin="-6,-3,-6,-3"
                                 />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

CS
Copy Code
gcSpreadSheet1.ActiveSheet.Cells["C1"].Value = 1;
gcSpreadSheet1.ActiveSheet.Cells["C2"].Value = 2;
gcSpreadSheet1.ActiveSheet.Cells["C3"].Value = 3;
gcSpreadSheet1.HighlightInvalidData = true;
var valid = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateFormulaListValidator("C1:C3");
gcSpreadSheet1.Sheets[0].Cells[0, 1].DataValidator = valid;
gcSpreadSheet1.Sheets[0].Cells[0, 0].Text = "Formulas";
VB.NET
Copy Code
GcSpreadSheet1.ActiveSheet.Cells("C1").Value = 1
GcSpreadSheet1.ActiveSheet.Cells("C2").Value = 2
GcSpreadSheet1.ActiveSheet.Cells("C3").Value = 3
GcSpreadSheet1.HighlightInvalidData = True
Dim valid = GrapeCity.Windows.SpreadSheet.Data.DataValidator.CreateFormulaListValidator("C1:C3")
GcSpreadSheet1.Sheets(0).Cells(0, 1).DataValidator = valid
GcSpreadSheet1.Sheets(0).Cells(0, 0).Text = "Formulas"
See Also