Back to Devexpress

GridCellValidationEventArgs.CellValue Property

wpf-devexpress-dot-xpf-dot-grid-dot-gridcellvalidationeventargs-0943d351.md

latest5.4 KB
Original Source

GridCellValidationEventArgs.CellValue Property

Gets the cell’s old valid value.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public object CellValue { get; }
vb
Public ReadOnly Property CellValue As Object

Property Value

TypeDescription
Object

An object that represents the cell’s old valid value.

|

Remarks

When handling the GridColumn.Validate event, the cell’s curernt value isn’t saved to a data source until it is validated. The event parameter’s CellValue property returns the cell’s old valid value. The current value which is being validated is returned by the event parameter’s Value property.

Example

This example shows how to validate the focused cell’s value. In this example, users cannot reduce the product’s price by more than 30% if the product is on discount.

View Example: How to Validate Cell Editors

xaml
<dxg:GridControl x:Name="grid">
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="ProductName">
            <dxg:GridColumn.EditSettings>
                <dxe:TextEditSettings AllowNullInput="False" />
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn FieldName="UnitPrice">
            <dxg:GridColumn.EditSettings>
                <dxe:SpinEditSettings DisplayFormat="c2" />
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
        <dxg:GridColumn FieldName="Discontinued" />
    </dxg:GridControl.Columns>
    <dxg:GridControl.View>
        <dxg:TableView AutoWidth="True"
                       ValidateCell="OnValidateCell" />
    </dxg:GridControl.View>
</dxg:GridControl>
cs
void OnValidateCell(object sender, GridCellValidationEventArgs e) {
    if(e.Column.FieldName != nameof(Product.UnitPrice) || !((Product)e.Row).Discontinued)
        return;
    var cellValue = (double)e.CellValue;
    var discount = 100 - Convert.ToDouble(e.Value) / cellValue * 100;
    if(discount > 0 && discount <= 30)
        return;

    e.IsValid = false;
    e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical;
    e.ErrorContent = discount < 0
        ? $"The price cannot be greater than {cellValue}"
        : $"The discount cannot be greater than 30% ({cellValue * 0.7}). Please correct the price.";
}
vb
Private Sub OnValidateCell(ByVal sender As Object, ByVal e As GridCellValidationEventArgs)
    If Not Equals(e.Column.FieldName, NameOf(Product.UnitPrice)) OrElse Not CType(e.Row, Product).Discontinued Then Return
    Dim cellValue = CDbl(e.CellValue)
    Dim discount = 100 - Convert.ToDouble(e.Value) / cellValue * 100
    If discount > 0 AndAlso discount <= 30 Then Return
    e.IsValid = False
    e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical
    e.ErrorContent = If(discount < 0, $"The price cannot be greater than {cellValue}", $"The discount cannot be greater than 30% ({cellValue * 0.7}). Please correct the price.")
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CellValue property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-data-grid-validate-cell-editors/CS/ValidateCell_CodeBehind/MainWindow.xaml.cs#L31

csharp
return;
var cellValue = (double)e.CellValue;
var discount = 100 - Convert.ToDouble(e.Value) / cellValue * 100;

wpf-data-grid-validate-cell-editors/VB/ValidateCell_CodeBehind/MainWindow.xaml.vb#L38

vb
If Not Equals(e.Column.FieldName, NameOf(Product.UnitPrice)) OrElse Not CType(e.Row, Product).Discontinued Then Return
Dim cellValue = CDbl(e.CellValue)
Dim discount = 100 - Convert.ToDouble(e.Value) / cellValue * 100

See Also

GridCellValidationEventArgs Class

GridCellValidationEventArgs Members

DevExpress.Xpf.Grid Namespace