{"record":{"id":"42301eb653c008ab","repo":"dotnet/wpf","slug":"sr-listview-notallowsharecolumntotwocolumncollection","errorCode":null,"errorMessage":"SR.ListView_NotAllowShareColumnToTwoColumnCollection","messagePattern":"SR\\.ListView_NotAllowShareColumnToTwoColumnCollection","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GridViewColumnCollection.cs","lineNumber":431,"sourceCode":"            }\n\n            return null;\n        }\n\n        private void VerifyIndexInRange(int index, [CallerArgumentExpression(nameof(index))] string indexName = null)\n        {\n            ArgumentOutOfRangeException.ThrowIfNegative(index, indexName);\n            ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _actualIndices.Count, indexName);\n        }\n\n        // Throw if column is null or already existed in a GVCC\n        private void ValidateColumnForInsert(GridViewColumn column)\n        {\n            ArgumentNullException.ThrowIfNull(column);\n\n            if (column.ActualIndex >= 0)\n            {\n                throw new InvalidOperationException(SR.ListView_NotAllowShareColumnToTwoColumnCollection);\n            }\n        }\n\n        private void VerifyAccess()\n        {\n            if (IsImmutable)\n            {\n                throw new InvalidOperationException(SR.ListView_GridViewColumnCollectionIsReadOnly);\n            }\n\n            // Although CheckReentrancy() is called in base class, we still need to call it here again,\n            // otherwise, when Reentrancy is found and exception is thrown, our operation is done and can't be undo.\n            CheckReentrancy();\n        }\n\n        #endregion\n\n        //------------------------------------------------------","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/GridViewColumnCollection.cs#L413-L449","documentation":"WPF throws this InvalidOperationException when the same GridViewColumn instance is inserted into a GridViewColumnCollection while it already belongs to one. Each column tracks its ActualIndex (>=0 once parented), and a column cannot be shared by two collections. It protects the invariant that a column has exactly one owning collection.","triggerScenarios":"Calling columnCollection.Insert/Add/Move (via InsertPreprocess -> ValidateColumnForInsert) with a GridViewColumn whose ActualIndex >= 0, i.e. a column that is already a member of another (or the same) GridViewColumnCollection.","commonSituations":"Reusing a single column array/field to populate two GridViews or ListViews; moving a column from one ListView to another without removing it first; sharing columns defined in XAML resources across multiple views.","solutions":["Remove the column from its current collection (or clone it) before inserting into the new collection","Create a new GridViewColumn instance per collection instead of sharing instances","If reusing layout, copy properties (Header, DisplayMemberBinding, Width) into fresh columns"],"exampleFix":"// before\nforeach (var col in sharedColumns) { otherView.Columns.Add(col); }\n// after\nforeach (var col in sharedColumns) { otherView.Columns.Add(CloneColumn(col)); } // clone, don't share","handlingStrategy":"validation","validationCode":"if (column.ActualIndex >= 0) throw new InvalidOperationException(\"Column already belongs to a collection; remove or clone it first.\");","typeGuard":"bool CanInsert(GridViewColumn c) => c is null || c.ActualIndex < 0;","tryCatchPattern":"try { collection.Insert(idx, column); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"column\")) { /* handle shared column */ }","preventionTips":["Never store GridViewColumn instances in shared statics or resources used by multiple views","Clone columns when reusing layout across views","Remove a column from its current collection before moving it"],"tags":["wpf","gridview","invalidoperation"],"backgroundTag":"unsupported-operation","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"}