{"record":{"id":"7b1ede46b8686e3e","repo":"dotnet/wpf","slug":"sr-datagrid-duplicatedisplayindex","errorCode":null,"errorMessage":"SR.DataGrid_DuplicateDisplayIndex","messagePattern":"SR\\.DataGrid_DuplicateDisplayIndex","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumnCollection.cs","lineNumber":497,"sourceCode":"            // 1. Validate all columns DisplayIndex\n            // 2. Add columns with DisplayIndex!=default to the assignedDisplayIndexMap\n            for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)\n            {\n                DataGridColumn currentColumn = this[columnIndex];\n                int currentColumnDisplayIndex = currentColumn.DisplayIndex;\n\n                ValidateDisplayIndex(currentColumn, currentColumnDisplayIndex);\n\n                if (currentColumn == changingColumn)\n                {\n                    currentColumnDisplayIndex = oldDisplayIndex;\n                }\n\n                if (currentColumnDisplayIndex >= 0)\n                {\n                    if (assignedDisplayIndexMap.ContainsKey(currentColumnDisplayIndex))\n                    {\n                        throw new ArgumentException(SR.DataGrid_DuplicateDisplayIndex);\n                    }\n\n                    assignedDisplayIndexMap.Add(currentColumnDisplayIndex, columnIndex);\n                }\n            }\n\n            // Second loop:\n            // Assign DisplayIndex to the columns with default values\n            int nextAvailableColumnIndex = 0;\n            for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)\n            {\n                DataGridColumn currentColumn = this[columnIndex];\n                int currentColumnDisplayIndex = currentColumn.DisplayIndex;\n                bool hasDefaultDisplayIndex = DataGridHelper.IsDefaultValue(currentColumn, DataGridColumn.DisplayIndexProperty);\n                if (currentColumn == changingColumn)\n                {\n                    if (oldDisplayIndex == -1)\n                    {","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridColumnCollection.cs#L479-L515","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nforeach (var col in columns)\n    col.DisplayIndex = 0; // duplicate\n// after\nfor (int i = 0; i < columns.Count; i++)\n    columns[i].DisplayIndex = i;","handlingStrategy":"validation","validationCode":"var indexes = grid.Columns.Select(c => c.DisplayIndex).Where(i => i >= 0).ToList();\nbool unique = indexes.Count == indexes.Distinct().Count();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["wpf","datagrid","displayindex","duplicate"],"backgroundTag":"conflicting-config-options","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}