{"record":{"id":"e1c21f8442aebcc2","repo":"dotnet/wpf","slug":"sr-datagrid-invalidcolumnreuse","errorCode":null,"errorMessage":"SR.DataGrid_InvalidColumnReuse","messagePattern":"SR\\.DataGrid_InvalidColumnReuse","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumnCollection.cs","lineNumber":45,"sourceCode":"            RebuildRealizedColumnsBlockListForNonVirtualizedRows = true;\n\n            RealizedColumnsBlockListForVirtualizedRows = null;\n            RealizedColumnsDisplayIndexBlockListForVirtualizedRows = null;\n            RebuildRealizedColumnsBlockListForVirtualizedRows = true;\n        }\n\n        #region Protected Overrides\n\n        protected override void InsertItem(int index, DataGridColumn item)\n        {\n            if (item == null)\n            {\n                throw new ArgumentNullException(nameof(item), SR.DataGrid_NullColumn);\n            }\n\n            if (item.DataGridOwner != null)\n            {\n                throw new ArgumentException(SR.Format(SR.DataGrid_InvalidColumnReuse, item.Header), nameof(item));\n            }\n\n            if (DisplayIndexMapInitialized)\n            {\n                ValidateDisplayIndex(item, item.DisplayIndex, true);\n            }\n\n            base.InsertItem(index, item);\n            item.CoerceValue(DataGridColumn.IsFrozenProperty);\n        }\n\n        protected override void SetItem(int index, DataGridColumn item)\n        {\n            if (item == null)\n            {\n                throw new ArgumentNullException(nameof(item), SR.DataGrid_NullColumn);\n            }\n","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumnCollection.cs#L27-L63","documentation":"DataGridColumnCollection.InsertItem throws ArgumentException with DataGrid_InvalidColumnReuse when the inserted column's DataGridOwner is already set, i.e. the column instance already belongs to another DataGrid. A DataGridColumn can only be hosted by one DataGrid at a time.","triggerScenarios":"Adding the same DataGridColumn instance (or one from another grid) to a second DataGrid.Columns collection; moving a column between grids without removing it first.","commonSituations":"Reusing a shared column instance across multiple grids; copying columns between grids by reference instead of creating new instances; moving columns between two grids on the same page.","solutions":["Create a new DataGridColumn instance for each grid","Remove the column from the previous grid's Columns before re-adding it elsewhere","Clone the column's settings into a fresh instance when sharing configuration","Never cache column instances for reuse across grids"],"exampleFix":"// before\nvar col = new DataGridTextColumn { Header = \"Name\", Binding = new Binding(\"Name\") };\ngridA.Columns.Add(col);\ngridB.Columns.Add(col); // reuse\n// after\ngridA.Columns.Add(new DataGridTextColumn { Header = \"Name\", Binding = new Binding(\"Name\") });\ngridB.Columns.Add(new DataGridTextColumn { Header = \"Name\", Binding = new Binding(\"Name\") });","handlingStrategy":"type-guard","validationCode":"if (column.DataGridOwner != null)\n    throw new InvalidOperationException(\"Column already belongs to a DataGrid; create a new instance.\");","typeGuard":"bool IsReusable(DataGridColumn c) => c.DataGridOwner == null;","tryCatchPattern":null,"preventionTips":["Never share DataGridColumn instances between grids","Remove a column from its current grid before moving it","Create per-grid column instances, even if configuration is copied"],"tags":["wpf","datagrid","column-reuse","invalid-argument"],"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"}