{"record":{"id":"2aa5950f317be625","repo":"dotnet/wpf","slug":"sr-datagridrow-cannotselectrowwhencells","errorCode":null,"errorMessage":"SR.DataGridRow_CannotSelectRowWhenCells","messagePattern":"SR\\.DataGridRow_CannotSelectRowWhenCells","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridItemAutomationPeer.cs","lineNumber":288,"sourceCode":"            get\n            {\n                return this.OwningDataGrid.SelectedItems.Contains(Item);\n            }\n        }\n\n        IRawElementProviderSimple ISelectionItemProvider.SelectionContainer\n        {\n            get\n            {\n                return ProviderFromPeer(_dataGridAutomationPeer);\n            }\n        }\n\n        void ISelectionItemProvider.AddToSelection()\n        {\n            if (!IsRowSelectionUnit)\n            {\n                throw new InvalidOperationException(SR.DataGridRow_CannotSelectRowWhenCells);\n            }\n\n            // If item is already selected - do nothing\n            object item = Item;\n            if (this.OwningDataGrid.SelectedItems.Contains(item))\n            {\n                return;\n            }\n\n            EnsureEnabled();\n\n            if (this.OwningDataGrid.SelectionMode == DataGridSelectionMode.Single &&\n                this.OwningDataGrid.SelectedItems.Count > 0)\n            {\n                throw new InvalidOperationException();\n            }\n\n            if (this.OwningDataGrid.Items.Contains(item))","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridItemAutomationPeer.cs#L270-L306","documentation":"ISelectionItemProvider.AddToSelection on a DataGrid row peer throws InvalidOperationException when the DataGrid's SelectionUnit is not FullRow (e.g. Cell or CellOrRowHeader). In cell-selection mode rows cannot be programmatically added to selection via the row peer.","triggerScenarios":"Calling AddToSelection via UIA on a DataGridRowAutomationPeer whose OwningDataGrid.SelectionUnit is DataGridSelectionUnit.Cell or CellOrRowHeader.","commonSituations":"UIA test suites written against default SelectionUnit (FullRow) then run against a grid configured with cell selection; accessibility clients attempting row selection in cell-mode grids.","solutions":["Set DataGrid.SelectionUnit to FullRow if row selection is required.","Select the specific cells instead of the row when SelectionUnit is cell-based.","Use the DataGrid's SelectedCells API directly instead of the row selection pattern.","Catch InvalidOperationException and branch on IsRowSelectionUnit before selecting."],"exampleFix":"// before\nselectionItemPattern.AddToSelection(); // cell selection mode\n// after\nif (dataGrid.SelectionUnit == DataGridSelectionUnit.FullRow)\n    selectionItemPattern.AddToSelection();\nelse\n    dataGrid.SelectedCells.Add(new DataGridCellInfo(item, dataGrid.Columns[0]));","handlingStrategy":"validation","validationCode":"if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)\n    throw new SkipRowSelectionException(); // or select cells instead","typeGuard":null,"tryCatchPattern":"try { selectionItemPattern.AddToSelection(); }\ncatch (InvalidOperationException) { dataGrid.SelectedCells.Add(new DataGridCellInfo(item, dataGrid.Columns[0])); }","preventionTips":["Verify SelectionUnit before using row-level selection patterns","Keep test automation aware of grid selection-mode configuration","Use SelectedCells API for cell-selection grids"],"tags":["wpf","uiautomation","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"}