dotnet/wpf · error · ArgumentException
SR.DataGrid_DuplicateDisplayIndex
Error message
SR.DataGrid_DuplicateDisplayIndex
What it means
DataGridColumnCollection.InitializeDisplayIndexMap throws ArgumentException with DataGrid_DuplicateDisplayIndex when two columns claim the same non-negative DisplayIndex during map initialization. DisplayIndex values must be unique across all columns of a DataGrid.
Solutions
- Ensure each column has a unique DisplayIndex before measure/init
- Assign DisplayIndex sequentially (0,1,2,...) when building columns in code
- Avoid setting DisplayIndex manually; let the DataGrid auto-assign when columns are added
- When reordering, update the full set of DisplayIndex values consistently
Example fix
// before
foreach (var col in columns)
col.DisplayIndex = 0; // duplicate
// after
for (int i = 0; i < columns.Count; i++)
columns[i].DisplayIndex = i; Defensive patterns
Strategy: validation
Validate before calling
var indexes = grid.Columns.Select(c => c.DisplayIndex).Where(i => i >= 0).ToList(); bool unique = indexes.Count == indexes.Distinct().Count();
Prevention
- Assign sequential unique DisplayIndex values in code
- Never copy-paste XAML DisplayIndex without adjusting values
- Let columns auto-assign DisplayIndex unless explicit order is required
When it happens
Trigger: Setting two columns' DisplayIndex to the same value; changing a column's DisplayIndex to one already taken (without the grid reassigning others); bulk-assigning indices in code before the map is built.
Common situations: Data-binding/generated columns all initialized with the same default DisplayIndex; programmatically reordering by assigning DisplayIndex without making values unique; XAML copy-paste leaving duplicate DisplayIndex attributes.
Understand the failure class
Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.
Related errors
- SR.DataGrid_ColumnDisplayIndexOutOfRange
- Can only call SelectAll when CanSelectMultipleItems is true.
- Container already has Starting Part.
- Exception of type 'System.InvalidOperationException' was…
- FixedDocument has more than one related document structure.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/7b1ede46b8686e3e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumnCollection.cs:497
// 1. Validate all columns DisplayIndex
// 2. Add columns with DisplayIndex!=default to the assignedDisplayIndexMap
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
{
DataGridColumn currentColumn = this[columnIndex];
int currentColumnDisplayIndex = currentColumn.DisplayIndex;
ValidateDisplayIndex(currentColumn, currentColumnDisplayIndex);
if (currentColumn == changingColumn)
{
currentColumnDisplayIndex = oldDisplayIndex;
}
if (currentColumnDisplayIndex >= 0)
{
if (assignedDisplayIndexMap.ContainsKey(currentColumnDisplayIndex))
{
throw new ArgumentException(SR.DataGrid_DuplicateDisplayIndex);
}
assignedDisplayIndexMap.Add(currentColumnDisplayIndex, columnIndex);
}
}
// Second loop:
// Assign DisplayIndex to the columns with default values
int nextAvailableColumnIndex = 0;
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
{
DataGridColumn currentColumn = this[columnIndex];
int currentColumnDisplayIndex = currentColumn.DisplayIndex;
bool hasDefaultDisplayIndex = DataGridHelper.IsDefaultValue(currentColumn, DataGridColumn.DisplayIndexProperty);
if (currentColumn == changingColumn)
{
if (oldDisplayIndex == -1)
{View on GitHub (pinned to 81131a70a4)