{"record":{"id":"1e156212a4dfe58a","repo":"dotnet/wpf","slug":"sr-selectedcellscollection-duplicateitem","errorCode":null,"errorMessage":"SR.SelectedCellsCollection_DuplicateItem","messagePattern":"SR\\.SelectedCellsCollection_DuplicateItem","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs","lineNumber":74,"sourceCode":"\n        /// <summary>\n        ///     Adds a cell to the list.\n        /// </summary>\n        /// <param name=\"cell\">The cell to add.</param>\n        public void Add(DataGridCellInfo cell)\n        {\n            _owner.Dispatcher.VerifyAccess();\n\n            ValidateIsReadOnly();\n\n            if (!IsValidPublicCell(cell))\n            {\n                throw new ArgumentException(SR.SelectedCellsCollection_InvalidItem, nameof(cell));\n            }\n\n            if (Contains(cell))\n            {\n                throw new ArgumentException(SR.SelectedCellsCollection_DuplicateItem, nameof(cell));\n            }\n\n            AddValidatedCell(cell);\n        }\n\n        /// <summary>\n        ///     Adds a validated cell to the list.\n        /// </summary>\n        /// <param name=\"cell\">The cell to add.</param>\n        internal void AddValidatedCell(DataGridCellInfo cell)\n        {\n            Debug.Assert(IsValidCell(cell), \"The cell should be valid.\");\n            Debug.Assert(!Contains(cell), \"VirtualizedCellInfoCollection does not support duplicate items.\");\n\n            int columnIndex;\n            int rowIndex;\n            ConvertCellInfoToIndexes(cell, out rowIndex, out columnIndex);\n            AddRegion(rowIndex, columnIndex, 1, 1);","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs#L56-L92","documentation":"DataGrid.SelectedCellsCollection.Add throws ArgumentException (SelectedCellsCollection_DuplicateItem) when the DataGridCellInfo being added is already present in the collection. The selected-cells collection is a set and rejects duplicates even though DataGridCellInfo is a lightweight value-like wrapper.","triggerScenarios":"Calling SelectedCells.Add twice with equivalent cells — e.g. the same (item, column) pair, or cell info from a row already selected — without checking Contains first, often in bulk-restore selection loops.","commonSituations":"Re-applying saved selection state on top of existing selection, or handling SelectionChanged/selection-restore logic that fires multiple times for the same cell.","solutions":["Check SelectedCells.Contains(cellInfo) before calling Add.","Call SelectedCells.Clear() before bulk-restoring a selection snapshot.","Use SelectAll or DeSelectAll helpers where they express intent better than per-cell adds."],"exampleFix":"// before\nforeach (var cell in savedCells) dg.SelectedCells.Add(cell);\n// after\ndg.SelectedCells.Clear();\nforeach (var cell in savedCells) dg.SelectedCells.Add(cell);","handlingStrategy":"validation","validationCode":"if (!dg.SelectedCells.Contains(cell)) dg.SelectedCells.Add(cell);","typeGuard":null,"tryCatchPattern":"try { dg.SelectedCells.Add(cell); } catch (ArgumentException) { /* already selected; ignore */ }","preventionTips":["Check Contains before Add","Clear the collection before bulk restore","De-duplicate selection snapshots before applying"],"tags":["wpf","datagrid","duplicate-item"],"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"}