Version

Working with Data Validation (XamSpreadsheet)

Purpose

This topic explains how to handle the data validation event and how to show circles around the cells with invalid data.

Required background

The following table lists the concept and topics required as a prerequisite to understanding this topic.

Type Content

Concept

Infragistics Excel Engine

Topic Purpose

This topic explains in details the features of the XamSpreadsheet control from developer perspective.

This topic explains what actions can be performed by the user in relation with the data validation feature.

This topic explains how to configure and set the build-in data validation rules.

In this topic

This topic contains the following sections:

When Validation Occurs

Summary

The XamSpreadsheet’s data validation occurs when a user performed cell editing is done. If multiple cells are affected (for example when confirming editing with Ctrl+Enter) only the active cell will be validated.

If you change the value of a cell as a result of other operations (for example selecting cell(s) and pressing Delete or pasting values) no validation will be performed.

Data Validation Event

The control supports a data validation-related event which is raised on cells which have validation rule assigned and when the user confirms a new input value which does not pass the validation. In the event arguments object of the event handler you have access to the edited cell and its validation rule. You can also implement your own logic which determines what action to be taken (accept, revert, stay in edit mode and show prompt) by the control.

Settings

The following table maps the desired task with the property from that manage it.

Task Use this property/event: And

Attach data validation event handler

Attach an event handler to it

Obtain the cell being edited

Read its value

Obtain the validation rule

Read its value

Obtain or set the control’s next action

Read or set its value of type SpreadsheetEditModeValidationErrorEventArgs

Note
Note

You can use the CanStayInEditMode property of the event arguments to check if the cell is currently allowed to stay in editing mode.

Example

The following code snippet demonstrates adding of a data validation event handler. The logic of this handler just checks if the new value can be parsed as integer and accepts the change:

In Visual Basic:

Me.XamSpreadsheet1.EditModeValidationError += EditModeValidationError_Handler
Public Sub EditModeValidationError_Handler(sender As Object, _
            e As SpreadsheetEditModeValidationErrorEventArgs)
    Dim obj = Me.XamSpreadsheet1.ActiveWorksheet.Rows(e.Cell.Row).Cells(e.Cell.Column).Value
    Dim number As Integer
    If Integer.TryParse(obj.ToString(), number) Then
        e.Action = SpreadsheetEditModeValidationErrorAction.AcceptChange
    Else
        e.Action = SpreadsheetEditModeValidationErrorAction.StayInEditMode
    End If
End Sub

In C#:

this.XamSpreadsheet1.EditModeValidationError += XamSpreadsheet_EditModeValidationError;
void XamSpreadsheet1_EditModeValidationError(object sender, SpreadsheetEditModeValidationErrorEventArgs e)
       {
           var obj = this.XamSpreadsheet1.ActiveWorksheet.Rows[e.Cell.Row].Cells[e.Cell.Column].Value;
           int number;
           if (int.TryParse(obj.ToString(), out number))
           {
               e.Action = SpreadsheetEditModeValidationErrorAction.AcceptChange;
           }
           else
           {
               e.Action = SpreadsheetEditModeValidationErrorAction.StayInEditMode;
           }
       }

Invalid Data Circles

Summary

The XamSpreadsheet control supports a visual notification option which draws a circle around each cell which has a validation rule set and its value does not pass the data validation.

The following screenshot shows an invalid value circle around one of the control’s cells:

xamSpreadsheet val1.png

Example

The following code snippet shows how to invoke the command which shows the invalid data circles:

In XAML:

<Button
    Content="Show Validation Circles"
    ig:Commanding.CommandTarget="{Binding ElementName=xamSpreadsheet1}">
    <ig:Commanding.Command>
        <igPrim:SpreadsheetCommandSource
            EventName="Click"
            CommandType="CircleInvalidData" />
    </ig:Commanding.Command>
</Button>
<ig:XamSpreadsheet x:Name="xamSpreadsheet1" />

The following code snippet shows how to invoke the command which hides the invalid data circles:

In XAML:

<Button
    Content="Clear Validation Circles"
    ig:Commanding.CommandTarget="{Binding ElementName=xamSpreadsheet1}">
    <ig:Commanding.Command>
        <igPrim:SpreadsheetCommandSource
            EventName="Click"
            CommandType="ClearValidationCircles" />
    </ig:Commanding.Command>
</Button>
<ig:XamSpreadsheet x:Name="xamSpreadsheet1" />

Related Content

The following topics provide additional information related to this topic.

Topic Purpose

This topic explains the activation feature from developer perspective.

This topic explains how to set or obtain active selection’s formatting.