{"record":{"id":"29dd90d4eb9e8843","repo":"dotnet/wpf","slug":"sr-datagrid-cannotselectcell-29dd90","errorCode":null,"errorMessage":"SR.DataGrid_CannotSelectCell","messagePattern":"SR\\.DataGrid_CannotSelectCell","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGrid.cs","lineNumber":4112,"sourceCode":"        {\n            get { return _selectedCells; }\n        }\n\n        /// <summary>\n        ///     Event that fires when the SelectedCells collection changes.\n        /// </summary>\n        public event SelectedCellsChangedEventHandler SelectedCellsChanged;\n\n        /// <summary>\n        ///     Direct notification from the SelectedCells collection of a change.\n        /// </summary>\n        internal void OnSelectedCellsChanged(NotifyCollectionChangedAction action, VirtualizedCellInfoCollection oldItems, VirtualizedCellInfoCollection newItems)\n        {\n            DataGridSelectionMode selectionMode = SelectionMode;\n            DataGridSelectionUnit selectionUnit = SelectionUnit;\n            if (!IsUpdatingSelectedCells && (selectionUnit == DataGridSelectionUnit.FullRow))\n            {\n                throw new InvalidOperationException(SR.DataGrid_CannotSelectCell);\n            }\n\n            // Update the pending list of changes\n            if (oldItems != null)\n            {\n                // When IsUpdatingSelectedCells is true, there may have been cells\n                // added to _pendingSelectedCells that are now being removed.\n                // These cells should be removed from _pendingSelectedCells and\n                // not added to _pendingUnselectedCells.\n                if (_pendingSelectedCells != null)\n                {\n                    VirtualizedCellInfoCollection.Xor(_pendingSelectedCells, oldItems);\n                }\n\n                if (_pendingUnselectedCells == null)\n                {\n                    _pendingUnselectedCells = oldItems;\n                }","sourceCodeStart":4094,"sourceCodeEnd":4130,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGrid.cs#L4094-L4130","documentation":"DataGrid throws this InvalidOperationException when cells are added to or removed from the SelectedCells collection while SelectionUnit is DataGridSelectionUnit.FullRow. In FullRow mode only entire rows can be selected, so individual cell selection state changes are illegal. The check is skipped while the DataGrid itself is updating SelectedCells internally (IsUpdatingSelectedCells).","triggerScenarios":"Directly mutating DataGrid.SelectedCells (Add/Remove/Clear) via OnSelectedCellsChanged or code-behind when SelectionMode/SelectionUnit is configured as FullRow; calling selection APIs on the collection instead of selecting rows.","commonSituations":"Developers switching a grid from CellOrRowHeader to FullRow selection but keeping old cell-selection code; custom selection logic written for cell-selection mode reused after changing SelectionUnit in XAML.","solutions":["Change SelectionUnit to DataGridSelectionUnit.CellOrRowHeader (or Cell) if cell selection is required","Replace cell-selection calls with row selection (set DataGridRow.IsSelected or use SelectedItems)","Wrap cell-selection mutation in a check of SelectionUnit before calling","Use SetSelectedCellsToBounds/DataGrid APIs rather than editing SelectedCells directly"],"exampleFix":"// before\nif (grid.SelectedCells.Count == 0) grid.SelectedCells.Add(new DataGridCellInfo(grid.Items[0]));\n// after\nif (grid.SelectionUnit != DataGridSelectionUnit.FullRow && grid.SelectedCells.Count == 0)\n    grid.SelectedCells.Add(new DataGridCellInfo(grid.Items[0]));\nelse\n    grid.SelectedIndex = 0;","handlingStrategy":"validation","validationCode":"if (grid.SelectionUnit == DataGridSelectionUnit.FullRow)\n    throw new InvalidOperationException(\"Cell selection is not allowed in FullRow selection mode.\");","typeGuard":"bool CanSelectCells(DataGrid g) => g.SelectionUnit != DataGridSelectionUnit.FullRow;","tryCatchPattern":"try { grid.SelectedCells.Add(cellInfo); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"select\")) { grid.SelectedIndex = cellInfo.Item is DataGridRow r ? r.GetIndex() : grid.Items.IndexOf(cellInfo.Item); }","preventionTips":["Match selection code to SelectionUnit configuration","Prefer SelectedItems/row-level APIs in FullRow mode","Revisit selection code whenever SelectionUnit changes in XAML"],"tags":["wpf","datagrid","selection","invalid-operation"],"backgroundTag":"invalid-state-transition","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"}