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

  1. Ensure each column has a unique DisplayIndex before measure/init
  2. Assign DisplayIndex sequentially (0,1,2,...) when building columns in code
  3. Avoid setting DisplayIndex manually; let the DataGrid auto-assign when columns are added
  4. 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

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


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)