Search code examples
c#winformsdevexpresserrorprovider

DxErrorProvider displays error message in messagebox when width changes


I'm working on data validation with DxErrorProvider class. Data is validated in GridControl's cells with help of ValidatingEditor event. It works fine, however one of the columns is very tight so when I want to show an error in that column's cell i try to change the width. Those actions makes DxErrorProvider (probably) show error in messagebox instead of nice circle with 'x' in cell.

Below you can see my implementation of event handler.

private void ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e)
{
    GridView view = sender as GridView;
    if (view.FocusedColumn.FieldName == "COLUMN1")
    {
        if (e.Value is bool)
        {
            GridColumn column = view.GetDataSourceItem<MyClass>(view.FocusedRowHandle);

            if (Names.Any(x => x.FieldName == column.FieldName) && !((bool)e.Value))
            {
                // These 2 lines make errorprovider go crazy
                //view.FocusedColumn.MaxWidth += 50;
                //view.FocusedColumn.Width += 30;
                e.Valid = false;
                e.ErrorText = "The error";
            }
            else
            {
                e.Valid = true;
                //view.FocusedColumn.Width -= 30;
                //view.FocusedColumn.MaxWidth -= 50;
            }
        }
        else
        {
            e.Valid = false;
            e.ErrorText = "Invalid value";
        }
    }
}

How can I preserve the default behavior of DxErrorProvider (the ( x )) and change column's width.


Solution

  • There is nothing to do with DxErrorProvider. The following steps describe the cause of the problem:

    0. When you are going to edit the cell the editor object is created in place of cell. This editor is based on BaseEdit class descendant and called in-place editor. You can get this editor through ColumnView.ActiveEditor property.
    1. When you validating the cell the in-place editor is actually validated and this nice circle with 'x' is shown in the editor instead of cell.
    2. But when you are changing the width of column, the editor is going to close, so there are no place to show the circle and this causes to show the message box instead.

    So, the simple workaround is to show the editor after changing the width of column. You can use the GridView.ShowEditor method for that. Also you need to put the validating value into this editor.
    Here is example:

    private void ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e)
    {
        GridView view = sender as GridView;
        if (view.FocusedColumn.FieldName == "COLUMN1")
        {
            if (e.Value is bool)
            {
                GridColumn column = view.GetDataSourceItem<MyClass>(view.FocusedRowHandle);
    
                if (Names.Any(x => x.FieldName == column.FieldName) && !((bool)e.Value))
                {
                    // These 2 lines make errorprovider go crazy
                    view.FocusedColumn.MaxWidth += 50;
                    view.FocusedColumn.Width += 30;
    
                    view.ShowEditor();
                    view.ActiveEditor.EditValue = e.Value;
    
                    e.Valid = false;
                    e.ErrorText = "The error";
                }
                else
                {
                    e.Valid = true;
    
                    view.FocusedColumn.Width -= 30;
                    view.FocusedColumn.MaxWidth -= 50;
                }
            }
            else
            {
                e.Valid = false;
                e.ErrorText = "Invalid value";
            }
        }
    }
    

    P.S.: Also, you can consider to use ColumnView.ValidateRow event instead.