Articles in this section
Category / Section

How to validate the AddNewRow value based on existing records in WPF DataGrid (SfDataGrid)?

4 mins read

You can validate the newly adding row value based on the existing records by using CurrentCellValidating event in WPF DataGrid (SfDataGrid).

C# 

this.sfGrid.CurrentCellValidating += sfGrid_CurrentCellValidating;
void sfGrid_CurrentCellValidating(object sender, Syncfusion.UI.Xaml.Grid.CurrentCellValidatingEventArgs args)
{
   bool isAddNewRow = sfGrid.IsAddNewIndex(sfGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex);
 
   if (!isAddNewRow)
      return;
 
   if (args.Column.MappingName == "Name")
   {
      var text = args.NewValue.ToString();
      var datacontext = (this.DataContext as UserInfoViewModel).UserDetails;
      var listOfNames = datacontext.Where(e => e.Name.Equals(text)).ToList();
      if (listOfNames.Count > 0)
      {
         args.ErrorMessage = "Entered Name is  already existing";
         args.IsValid = false;
      }
   }
}

You can also use RowValidating event to validate the row when the cell is edited. The RowValidating event occurs when the edited cells try to commit the row data or lose the focus.

Validate the new row based on existing records in WPF DataGrid

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