{"record":{"id":"9e464fdbb6556d69","repo":"dotnet/wpf","slug":"sr-removingplaceholder","errorCode":null,"errorMessage":"SR.RemovingPlaceholder","messagePattern":"SR\\.RemovingPlaceholder","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs","lineNumber":897,"sourceCode":"        /// Remove the given item from the underlying collection.\n        /// </summary>\n        public void Remove(object item)\n        {\n            if (IsEditingItem || IsAddingNew)\n                throw new InvalidOperationException(SR.Format(SR.MemberNotAllowedDuringAddOrEdit, \"Remove\"));\n            VerifyRefreshNotDeferred();\n\n            int index = InternalIndexOf(item);\n            if (index >= 0)\n            {\n                RemoveImpl(item, index);\n            }\n        }\n\n        private void RemoveImpl(object item, int index)\n        {\n            if (item == CollectionView.NewItemPlaceholder)\n                throw new InvalidOperationException(SR.RemovingPlaceholder);\n\n            BindingOperations.AccessCollection(InternalList,\n                () =>\n                {\n                    ProcessPendingChanges();\n\n                    // the pending changes may have moved (or even removed) the\n                    // item.   Verify the index.\n                    if (index >= InternalList.Count || !System.Windows.Controls.ItemsControl.EqualsEx(item, GetItemAt(index)))\n                    {\n                        index = InternalList.IndexOf(item);\n                        if (index < 0)\n                            return;\n                    }\n\n                    // convert the index from \"view-relative\" to \"list-relative\"\n                    if (_isGrouping)\n                    {","sourceCodeStart":879,"sourceCodeEnd":915,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingListCollectionView.cs#L879-L915","documentation":"RemoveImpl throws InvalidOperationException with the RemovingPlaceholder message when the caller attempts to remove the CollectionView.NewItemPlaceholder item. The placeholder is a virtual marker row (shown when NewItemPlaceholderPosition is set), not a real item in the collection, so it can never be removed.","triggerScenarios":"Calling Remove(CollectionView.NewItemPlaceholder) or RemoveAt(indexOfPlaceholder) — commonly when code enumerates the view and tries to delete 'all visible rows' including the placeholder row, or a user deletes the placeholder row in a grid.","commonSituations":"DataGrid bound to a view with NewItemPlaceholderPosition.AtBeginning/AtEnd and a delete handler acting on the selected item without checking for the placeholder; clearing loops that iterate the view's items directly.","solutions":["Check for CollectionView.NewItemPlaceholder and skip it before calling Remove/RemoveAt.","Enumerate the underlying collection instead of the view when doing bulk removals, so the placeholder never appears.","In DataGrid delete handlers, guard: if (view.IndexOf(item) is the placeholder or item == CollectionView.NewItemPlaceholder) return.","Disable delete UI for the placeholder row via a row style/validation."],"exampleFix":"// before\nview.Remove(selectedItem);\n\n// after\nif (!Equals(selectedItem, CollectionView.NewItemPlaceholder))\n    view.Remove(selectedItem);","handlingStrategy":"type-guard","validationCode":"// C#\nif (Equals(item, CollectionView.NewItemPlaceholder)) return; // skip","typeGuard":"bool IsPlaceholder(object item) => Equals(item, CollectionView.NewItemPlaceholder);","tryCatchPattern":"try { view.Remove(item); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"placeholder\")) { /* skip placeholder */ }","preventionTips":["Filter out CollectionView.NewItemPlaceholder in delete handlers.","Enumerate the source collection for bulk deletes, not the view.","Disable delete on the placeholder row in the UI."],"tags":["wpf","data-binding","placeholder"],"backgroundTag":"invalid-argument-value","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"}