{"record":{"id":"4691a00c12544dbc","repo":"dotnet/wpf","slug":"sr-format-sr-membernotallowedduringaddoredit-customfilter","errorCode":null,"errorMessage":"SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"CustomFilter\")","messagePattern":"SR\\.Format\\(SR\\.MemberNotAllowedDuringAddOrEdit, \"CustomFilter\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs","lineNumber":306,"sourceCode":"        /// <summary>\n        /// Gets or sets the filter to be used to exclude items from the collection of items returned by the data source .\n        /// </summary>\n        /// <remarks>\n        /// Before assigning, test if this CollectionView supports custom filtering\n        /// <seealso cref=\"CanCustomFilter\"/>.\n        /// The actual syntax depends on the implementer of IBindingListView. ADO's DataView is\n        /// a common example, see System.Data.DataView.RowFilter for its supported\n        /// filter expression syntax.\n        /// </remarks>\n        public string CustomFilter\n        {\n            get { return _customFilter; }\n            set\n            {\n                if (!CanCustomFilter)\n                    throw new NotSupportedException(SR.BindingListCannotCustomFilter);\n                if (IsAddingNew || IsEditingItem)\n                    throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"CustomFilter\"));\n                if (AllowsCrossThreadChanges)\n                    VerifyAccess();\n\n                _customFilter = value;\n\n                RefreshOrDefer();\n            }\n        }\n\n        /// <summary>\n        /// Test if this CollectionView supports custom filtering before assigning\n        /// a filter string to <seealso cref=\"CustomFilter\"/>.\n        /// </summary>\n        public bool CanCustomFilter\n        {\n            get\n            {\n                return ((_blv != null) && _blv.SupportsFiltering);","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L288-L324","documentation":"The CustomFilter setter throws InvalidOperationException when the view is in the middle of an AddNew transaction (IsAddingNew) or an item edit transaction (IsEditingItem). Structural view changes like changing the filter are not allowed during add/edit, so WPF rejects them with MemberNotAllowedDuringAddOrEdit naming 'CustomFilter'.","triggerScenarios":"Setting BindingListCollectionView.CustomFilter while CollectionView.AddNew/AddNew has an outstanding new item, or while EditItem has not been committed/cancelled (e.g. from a UI event while the DataGrid row is in edit mode).","commonSituations":"Applying or clearing a filter from a toolbar button while the grid's current row is being edited; filter logic in response to user input firing during a pending AddNew; automation/tests that change the filter without ending edit transactions.","solutions":["Commit or cancel the current transaction first: call CommitNew()/CancelNew() (and CommitEdit/CancelEdit) before setting CustomFilter.","Check IsAddingNew / IsEditingItem before assigning CustomFilter and defer the change until the transaction completes.","Set the filter before the user begins adding/editing rows (e.g. at view construction)."],"exampleFix":"// before\nview.CustomFilter = \"Amount > 100\"; // may throw during row edit\n\n// after\nif (view.IsAddingNew) view.CommitNew();\nif (view.IsEditingItem) view.CommitEdit();\nview.CustomFilter = \"Amount > 100\";","handlingStrategy":"validation","validationCode":"if (view.IsAddingNew) view.CommitNew();\nif (view.IsEditingItem) view.CommitEdit();\nview.CustomFilter = filter;","typeGuard":null,"tryCatchPattern":"try { view.CustomFilter = filter; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"edit\"))\n{\n    pendingFilter = filter; // apply after transaction completes\n}","preventionTips":["Check IsAddingNew/IsEditingItem before view mutations","Commit or cancel transactions in UI event handlers before filtering","Defer filter changes to transaction completion"],"tags":["wpf","collection-view","filtering","invalid-state"],"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"}