protected static void DataGridRowEditEnding(object eventArgs) { var tempEventArgs = (object[])eventArgs; if (!(tempEventArgs[0] is DataGridRowEditEndingEventArgs e && tempEventArgs[1] is DataGrid sender)) { throw new ArgumentException("Bad arguments in PreviewKeyDown"); } if (e.EditAction == DataGridEditAction.Cancel || !(e.Row.Item is TEntity entity) || !entity.Validator.IsValid || !entity.IsDirty) { return; } using (var context = new BookOrdersContext()) { try { if (e.Row.IsNewItem) { PropertyInfo property = context.GetType().GetProperty(typeof(TEntity).Name.Replace("And", "sAnd") + 's'); if (property != null) { ((DbSet <TEntity>)property.GetValue(context)).Add(entity); } } else { context.Entry(entity).State = EntityState.Modified; } context.SaveChanges(); } catch (Exception exception) { sender.CancelEdit(); sender.CancelEdit(); Exception innerException = exception; while (innerException?.InnerException != null) { innerException = innerException.InnerException; } MessageBox.Show(innerException?.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } }
private static void DataGridRowEditEnding(DataGridRowEditEndingEventArgs e) { if (e.EditAction == DataGridEditAction.Cancel || !(e.Row.Item is Genre genre) || !genre.Validator.IsValid || !genre.IsDirty) { return; } using (var context = new BookOrdersContext()) { if (e.Row.IsNewItem) { context.Genres.Add(genre); } else { context.Entry(genre).State = EntityState.Modified; } context.SaveChanges(); } }