{"record":{"id":"e12188af32018e75","repo":"dotnet/wpf","slug":"sr-selectedcellscollection-invaliditem","errorCode":null,"errorMessage":"SR.SelectedCellsCollection_InvalidItem","messagePattern":"SR\\.SelectedCellsCollection_InvalidItem","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs","lineNumber":69,"sourceCode":"        }\n\n        #endregion\n\n        #region IList<T> Members\n\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.\");","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizedCellInfoCollection.cs#L51-L87","documentation":"DataGrid.SelectedCellsCollection.Add throws ArgumentException (SelectedCellsCollection_InvalidItem) when the DataGridCellInfo passed does not belong to the DataGrid's items/columns — IsValidPublicCell checks that the cell references a valid item and column of the owning DataGrid.","triggerScenarios":"Calling dataGrid.SelectedCells.Add(cellInfo) with a DataGridCellInfo constructed from an item not in the grid's Items, a column not in its Columns, or a cellInfo from another DataGrid instance.","commonSituations":"Programmatically restoring selection state persisted from another grid or session, or building DataGridCellInfo manually with mismatched item/column references after the grid was re-bound.","solutions":["Build DataGridCellInfo only from (item, column) pairs that exist in the target DataGrid's Items and Columns collections.","Re-resolve saved selections against the current grid after rebinding instead of reusing stale DataGridCellInfo objects.","Avoid sharing cell info objects across DataGrid instances."],"exampleFix":"// before\ncell = new DataGridCellInfo(otherGridItem, column);\ndg.SelectedCells.Add(cell);\n// after\nvar item = dg.Items.Contains(otherGridItem) ? otherGridItem : null;\nvar col = dg.Columns.Contains(column) ? column : null;\nif (item != null && col != null) dg.SelectedCells.Add(new DataGridCellInfo(item, col));","handlingStrategy":"validation","validationCode":"if (dg.Items.Contains(item) && dg.Columns.Contains(column))\n    dg.SelectedCells.Add(new DataGridCellInfo(item, column));","typeGuard":"bool IsValidCell(DataGridCellInfo c, DataGrid dg) => dg.Items.Contains(c.Item) && (c.Column == null || dg.Columns.Contains(c.Column));","tryCatchPattern":"try { dg.SelectedCells.Add(cell); } catch (ArgumentException) { /* cell not in this grid; skip */ }","preventionTips":["Only build DataGridCellInfo from the target grid's own Items and Columns","Re-resolve persisted selections after rebinding","Never share cell info across DataGrid instances"],"tags":["wpf","datagrid","argument-exception"],"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"}