Articles in this section
Category / Section

How to remove the top-right corner error mark from the GridCell in WPF DataGrid?

2 mins read

In the SfDataGrid, validation can be done by handling the CurrentCellValidating event. The SfDataGrid does not allow the end-user to move to the other cells until the validation succeeds. In a meantime, you can move to the other cells after pressing Esc key that resets the validation status and error message at the top-right corner.

 

//Handles the CurrentCellValidating event.
SfdataGrid.CurrentCellValidating += SfdataGrid_CurrentCellValidating;
void SfdataGrid_CurrentCellValidating(object sender, CurrentCellValidatingEventArgs args)
{
    if (args.Column.MappingName == "EmployeeSalary" && Convert.ToDouble(args.NewValue) < 20000)
    {
        args.ErrorMessage = "EmployeeSalary should not less than 20000";
        args.IsValid = false;
    }
}

 

It is possible to reset the validation status when the end-user presses Esc key from the edit mode by overriding the GridSelectionController. In the following code example, the validation status and top-right corner error indication is reset by invoking the ValidationHelper.ResetValidations method on pressing the Esc key by overriding the ProcessKeyDown method of the GridSelectionController.

 

For WPF:

using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.Grid.Helpers;
// Sets the instance of the customized GridSelectionController class to the SfDataGird.SelectionController property.
SfdataGrid.SelectionController = new GridSelectionControllerExt(SfdataGrid);
public class GridSelectionControllerExt : GridSelectionController
{
    public GridSelectionControllerExt(SfDataGrid dataGrid)
        : base(dataGrid)
    {
    }
    protected override void ProcessKeyDown(KeyEventArgs args)
    {
        if (this.CurrentCellManager.HasCurrentCell && this.CurrentCellManager.CurrentCell.IsEditing && args.Key == Key.Escape)
        {
            var helper = DataGrid.GetValidationHelper();
            MethodInfo[] methodInfos = typeof(ValidationHelper).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
            // Gets the ResetValidations method by using reflection.
            var resetValidation = methodInfos.FirstOrDefault(item => item.Name == "ResetValidations");
            resetValidation.Invoke(helper, new object[] { true });
            this.CurrentCellManager.EndEdit(false);
            return;
        }
        base.ProcessKeyDown(args);
    }
}

 

For WinRT:

public class GridSelectionControllerExt : GridSelectionController
{
    public GridSelectionControllerExt(SfDataGrid dataGrid)
        : base(dataGrid)
    {
    }
    protected override void ProcessKeyDown(KeyRoutedEventArgs args)
    {     
        if (this.CurrentCellManager.HasCurrentCell && this.CurrentCellManager.CurrentCell.IsEditing && args.Key == VirtualKey.Escape)
        {
            var helper = DataGrid.GetValidationHelper();
            // Gets the ResetValidations method by using reflection.
            foreach (var method in helper.GetType().GetRuntimeMethods())
            {
                if (method.Name.Equals("ResetValidations"))
                {
                    method.Invoke(helper, new object[] { true });
                    this.CurrentCellManager.EndEdit(false);
                    return;
                }
            }      
        }
        base.ProcessKeyDown(args);
    }
}

 

Note:

You need to override the GridCellSelectionController class when the SelectionUnit is GridSelecionUnit.Cell.

 

The corner error mark is enabled when the employeesalary less 20000 as shown in following screenshot

employeeSalary

Sample Links:

WPF

WinRT

UWP

 

Conclusion

I hope you enjoyed learning about how to remove the top-right corner error mark from the GridCell in WPF DataGrid.

You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied