dotnet/wpf · error · ArgumentException
SR.DataGrid_InvalidColumnReuse
Error message
SR.DataGrid_InvalidColumnReuse
What it means
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.
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
Example fix
// before
var col = new DataGridTextColumn { Header = "Name", Binding = new Binding("Name") };
gridA.Columns.Add(col);
gridB.Columns.Add(col); // reuse
// after
gridA.Columns.Add(new DataGridTextColumn { Header = "Name", Binding = new Binding("Name") });
gridB.Columns.Add(new DataGridTextColumn { Header = "Name", Binding = new Binding("Name") }); Defensive patterns
Strategy: type-guard
Validate before calling
if (column.DataGridOwner != null)
throw new InvalidOperationException("Column already belongs to a DataGrid; create a new instance."); Type guard
bool IsReusable(DataGridColumn c) => c.DataGridOwner == null;
Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.PropertyNotSupported
- Animation_ChildMustBeKeyFrame
- Animation_DependencyPropertyIsNotAnimatable
- Animation_UnrecognizedHandoffBehavior
- Can only call SelectAll when CanSelectMultipleItems is true.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e1c21f8442aebcc2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumnCollection.cs:45
RebuildRealizedColumnsBlockListForNonVirtualizedRows = true;
RealizedColumnsBlockListForVirtualizedRows = null;
RealizedColumnsDisplayIndexBlockListForVirtualizedRows = null;
RebuildRealizedColumnsBlockListForVirtualizedRows = true;
}
#region Protected Overrides
protected override void InsertItem(int index, DataGridColumn item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item), SR.DataGrid_NullColumn);
}
if (item.DataGridOwner != null)
{
throw new ArgumentException(SR.Format(SR.DataGrid_InvalidColumnReuse, item.Header), nameof(item));
}
if (DisplayIndexMapInitialized)
{
ValidateDisplayIndex(item, item.DisplayIndex, true);
}
base.InsertItem(index, item);
item.CoerceValue(DataGridColumn.IsFrozenProperty);
}
protected override void SetItem(int index, DataGridColumn item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item), SR.DataGrid_NullColumn);
}
View on GitHub (pinned to 81131a70a4)