Articles in this section
Category / Section

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

1 min read

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.

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