How to: Validate Modified Rows

  • 2 minutes to read

The example below tracks changes made to the “Units In Stock” and “Units On Order” columns. If an edited record has its “In Stock” value less than “On Order”, this row is considered invalid. In this case the e.Valid parameter of the ColumnView.ValidateRow event is set to false.

The default warning message does not pop up since the ExceptionMode parameter of the ColumnView.InvalidRowException event is set to NoAction. Instead, the ColumnView.SetColumnError method displays error icons in both cells.

ColumnView_ValidateRow

using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;

private void gridView1_ValidateRow(object sender, 
DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) {
    GridView view = sender as GridView;
    GridColumn inStockCol = view.Columns["UnitsInStock"];
    GridColumn onOrderCol = view.Columns["UnitsOnOrder"];
    //Get the value of the first column
    Int16 inSt = (Int16)view.GetRowCellValue(e.RowHandle, inStockCol);
    //Get the value of the second column
    Int16 onOrd = (Int16)view.GetRowCellValue(e.RowHandle, onOrderCol);
    //Validity criterion
    if (inSt < onOrd) {
        e.Valid = false;
        //Set errors with specific descriptions for the columns
        view.SetColumnError(inStockCol, "The value must be greater than Units On Order");
        view.SetColumnError(onOrderCol, "The value must be less than Units In Stock");
    }
}

private void gridView1_InvalidRowException(object sender, 
DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e) {
    //Suppress displaying the error message box
    e.ExceptionMode = ExceptionMode.NoAction;
}