GridView Column validation, display error message don't loose focus

c#, devexpress, gridview, winforms

Solution

Source

I am afraid it is difficult to determine the cause of the issue based on the provided information. Generally, the `ValidatingEditor` event fires when an active editor is closed, and its value is changed. The `ValidateRow` event fires when a `current row loses focus`, and some of its values has been changed.

I suggest you to go through following links: Validating Rows Validating Editors BaseView.ValidatingEditor Event

Check this example:

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;

private void gridView1_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e) {
    GridView view = sender as GridView;
    if(view.FocusedColumn.FieldName == "Discount") {
        //Get the currently edited value 
        double discount = Convert.ToDouble(e.Value);
        //Specify validation criteria 
        if(discount < 0) {
            e.Valid = false;
            e.ErrorText = "Enter a positive value";
        }
        if(discount > 0.2) {
            e.Valid = false;
            e.ErrorText = "Reduce the amount (20% is maximum)";
        }
    }
}

private void gridView1_InvalidValueException(object sender, InvalidValueExceptionEventArgs e) {
    //Do not perform any default action 
    e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
    //Show the message with the error text specified 
    MessageBox.Show(e.ErrorText);
}

Editor validation takes place when attempting to save the edit value (using the `PostEditor` method) or when closing the active editor. First, the automatic validation is performed. The editor determines whether it can accept the entered value. After the automatic validation has been performed, the `ValidatingEditor` event is raised. Handle this event to implement custom constraints on cell values.

If your condition fails then set the `e.Valid = false;` and the `e.ErrorText = "ColumnSum must be <= 100";` and also handle the `InvalidValueException` event of gridview.

Problem

I need to validate a gridView column such as the sum of all elements in the column are <=100; If the user enters a value and the sum exceeds the limit i want to display a custom error message. I've tried using this event on the column's repositoryedit: ``` void pinEditRepositoryItem_Validating(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; gridview1.SetColumnError(m_imixGridView.Columns["MyColumn"], "ColumnSum must be <= 100", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical); } ``` However, when setting ``` e.Cancel = true; ``` I get the default message "Invalid Value". If i use ``` gridview1.SetColumnError(m_imixGridView.Columns["MyColumn"], "ColumnSum must be <= 100", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical); ``` only, the error message is correct but if i click outside the focus is lost. I've seen that there are multiple methods for validating rows but didn't find a solution that will suit best in this case. Is it possible to disable 'unfocus' on a cell if the validation fails? Thanks a lot!

Original source