{"record":{"id":"ecdaf713374f99d3","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringaddoredit-customsort","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"CustomSort\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringAddOrEdit, \"CustomSort\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":461,"sourceCode":"        #endregion ICollectionView\n\n        /// <summary>\n        /// Set a custom comparer to sort items using an object that implements IComparer.\n        /// </summary>\n        /// <remarks>\n        /// Setting the Sort criteria has no immediate effect,\n        /// an explicit <seealso cref=\"CollectionView.Refresh\"/> call by the app is required.\n        /// Note: Setting the custom comparer object will clear previously set <seealso cref=\"CollectionView.SortDescriptions\"/>.\n        /// </remarks>\n        public IComparer CustomSort\n        {\n            get { return _customSort; }\n            set\n            {\n                if (AllowsCrossThreadChanges)\n                    VerifyAccess();\n                if (IsAddingNew || IsEditingItem)\n                    throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"CustomSort\"));\n                _customSort = value;\n\n                SetSortDescriptions(null);\n\n                RefreshOrDefer();\n            }\n        }\n\n        /// <summary>\n        /// A delegate to select the group description as a function of the\n        /// parent group and its level.\n        /// </summary>\n        [DefaultValue(null)]\n        public virtual GroupDescriptionSelectorCallback GroupBySelector\n        {\n            get { return _group.GroupBySelector; }\n            set\n            {","sourceCodeStart":443,"sourceCodeEnd":479,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L443-L479","documentation":"ListCollectionView.CustomSort cannot be assigned while an AddNew or EditItem transaction is open. Setting CustomSort triggers a sort/refresh, which the view forbids mid-transaction, so it throws InvalidOperationException.","triggerScenarios":"Setting the CustomSort property (IComparer) after AddNew() or EditItem() started a transaction that was not committed or cancelled.","commonSituations":"Applying custom sorting from UI events (column header clicks, combo selection) while a DataGrid is adding or editing a row; code that reassigns the comparer after data edits without closing the transaction.","solutions":["Commit or cancel the pending transaction (CommitNew/CommitEdit/CancelNew/CancelEdit) before setting CustomSort","Guard the assignment with IsAddingNew/IsEditingItem checks","Reapply the CustomSort after the transaction ends, e.g. on CollectionView transaction-completion events"],"exampleFix":"// before\nview.CustomSort = comparer; // throws during AddNew\n// after\nif (view.IsAddingNew) view.CommitNew();\nif (view is IEditableCollectionView ecv && ecv.IsEditingItem) ecv.CommitEdit();\nview.CustomSort = comparer;","handlingStrategy":"validation","validationCode":"var ecv = collectionView as IEditableCollectionView;\nif (!(collectionView.IsAddingNew || (ecv != null && ecv.IsEditingItem)))\n    collectionView.CustomSort = comparer;","typeGuard":null,"tryCatchPattern":"try { view.CustomSort = comparer; }\ncatch (InvalidOperationException)\n{\n    // postpone sort until the current transaction completes\n}","preventionTips":["Check IsAddingNew/IsEditingItem before any sort/filter/group mutation","Commit or cancel DataGrid edits before applying new sort comparers","Queue view-setting changes and apply them after transaction completion"],"tags":["wpf","data-binding","invalidoperationexception","listcollectionview","sorting"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}