{"record":{"id":"a723d478dae811d8","repo":"dotnet/wpf","slug":"sr-datagrid-cannotselectcell","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/Automation/Peers/DataGridCellItemAutomationPeer.cs","lineNumber":687,"sourceCode":"            get\n            {\n                return this.OwningDataGrid.SelectedCellsInternal.Contains(new DataGridCellInfo(Item, _column));\n            }\n        }\n\n        IRawElementProviderSimple ISelectionItemProvider.SelectionContainer\n        {\n            get\n            {\n                return this.ContainingGrid;\n            }\n        }\n\n        void ISelectionItemProvider.AddToSelection()\n        {\n            if (!IsCellSelectionUnit)\n            {\n                throw new InvalidOperationException(SR.DataGrid_CannotSelectCell);\n            }\n\n            // If item is already selected - do nothing\n            DataGridCellInfo currentCellInfo = new DataGridCellInfo(Item, _column);\n            if (this.OwningDataGrid.SelectedCellsInternal.Contains(currentCellInfo))\n            {\n                return;\n            }\n\n            EnsureEnabled();\n\n            if (this.OwningDataGrid.SelectionMode == DataGridSelectionMode.Single &&\n                this.OwningDataGrid.SelectedCells.Count > 0)\n            {\n                throw new InvalidOperationException();\n            }\n\n            this.OwningDataGrid.SelectedCellsInternal.Add(currentCellInfo);","sourceCodeStart":669,"sourceCodeEnd":705,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridCellItemAutomationPeer.cs#L669-L705","documentation":"DataGridCellItemAutomationPeer's ISelectionItemProvider.AddToSelection can only add cells to selection when the DataGrid's SelectionUnit is Cell (or cell-level selection is meaningful). If the DataGrid is not in cell selection mode, the peer throws InvalidOperationException with SR.DataGrid_CannotSelectCell because full-row selection cannot accommodate cell-based selection requests.","triggerScenarios":"An automation client calls AddToSelection() on a DataGrid cell while DataGrid.SelectionUnit is FullRow (or the peer's IsCellSelectionUnit check fails for another reason such as a shared/placeholder item).","commonSituations":"UIA tests written against DataGrids configured with SelectionUnit=\"Cell\" being run against one with SelectionUnit=\"FullRow\" (default); theme or control-template changes; automated accessibility tooling selecting cells.","solutions":["Set DataGrid.SelectionUnit=\"Cell\" when cell-level automation selection is required.","Change the automation client to use row/row-item selection patterns when SelectionUnit is FullRow.","Catch InvalidOperationException and fall back to selecting the row containing the cell.","Check SelectionUnit before invoking selection APIs in test code."],"exampleFix":"// before\ncellSelectionItem.AddToSelection();\n\n// after\nif (dataGrid.SelectionUnit == DataGridSelectionUnit.Cell)\n    cellSelectionItem.AddToSelection();\nelse\n    rowSelectionItem.Select();","handlingStrategy":"validation","validationCode":"bool canAdd = dataGrid.SelectionUnit == DataGridSelectionUnit.Cell;","typeGuard":null,"tryCatchPattern":"try { peer.AddToSelection(); } catch (InvalidOperationException) { rowPeer.Select(); }","preventionTips":["Match automation scripts to DataGrid SelectionUnit","Set SelectionUnit=Cell in test fixtures needing cell selection","Fall back to row selection on failure"],"tags":["wpf","automation","datagrid","selection"],"backgroundTag":"unsupported-operation","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"}