We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to wire the RowValidating event after pasted content to WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid
Tags: validation

The row validation event is raised on the validation state of the row editing in WPF DataGrid (SfDataGrid) and is not on the content pasted in the row. You can fire the row validating event after pasting the content by marking the current row validated flag as false using the ValidationHelper.SetCurrentRowValidated method in PasteGridCellContent event.

SfDataGridBehavior.cs

Public class SfDataGridBehavior :Behavior<SfDataGrid>
{
    SfDataGrid dataGrid = null;
        
    protected override void OnAttached()
    {
        dataGrid = this.AssociatedObject as SfDataGrid;
        dataGrid.RowValidating += DataGrid_RowValidating;
        dataGrid.PasteGridCellContent += DataGrid_PasteGridCellContent;
    }
    private void DataGrid_RowValidating(object sender, RowValidatingEventArgs e)
    {
         //You can write your own code here
    }
    private void DataGrid_PasteGridCellContent(object sender, GridCopyPasteCellEventArgs e)
    {
        this.dataGrid.GetValidationHelper().SetCurrentRowValidated(false);
    }
}

In the sample, the EmployeeName is validated by using the following code snippets.

private void DataGrid_RowValidating(object sender, RowValidatingEventArgs e)
{
    var employee = e.RowData as Employee;
    if (employee.EmployeeName == "Null" || employee.EmployeeName == "DbNull")
    {
        e.IsValid = false;
        e.ErrorMessages.Add("EmployeeName", "EmployeeName must have a value and it should not be null");
    }
}

View sample in GitHub.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile