{"record":{"id":"a2d51b0e09920714","repo":"dotnet/wpf","slug":"sr-datagridrow-cannotselectrowwhencells-a2d51b","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/Controls/DataGridRow.cs","lineNumber":1054,"sourceCode":"            get { return (bool)GetValue(IsSelectedProperty); }\n            set { SetValue(IsSelectedProperty, value); }\n        }\n\n        /// <summary>\n        ///     The DependencyProperty for the IsSelected property.\n        /// </summary>\n        public static readonly DependencyProperty IsSelectedProperty = Selector.IsSelectedProperty.AddOwner(\n            typeof(DataGridRow),\n            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, new PropertyChangedCallback(OnIsSelectedChanged)));\n\n        private static void OnIsSelectedChanged(object sender, DependencyPropertyChangedEventArgs e)\n        {\n            DataGridRow row = (DataGridRow)sender;\n            bool isSelected = (bool)e.NewValue;\n\n            if (isSelected && !row.IsSelectable)\n            {\n                throw new InvalidOperationException(SR.DataGridRow_CannotSelectRowWhenCells);\n            }\n\n            DataGrid grid = row.DataGridOwner;\n            if (grid != null && row.DataContext != null)\n            {\n                DataGridAutomationPeer gridPeer = UIElementAutomationPeer.FromElement(grid) as DataGridAutomationPeer;\n                if (gridPeer != null)\n                {\n                    DataGridItemAutomationPeer rowItemPeer = gridPeer.FindOrCreateItemAutomationPeer(row.DataContext) as DataGridItemAutomationPeer;\n                    rowItemPeer?.RaisePropertyChangedEvent(\n                            System.Windows.Automation.SelectionItemPatternIdentifiers.IsSelectedProperty,\n                            (bool)e.OldValue,\n                            isSelected);\n                }\n            }\n\n            // Update the header's IsRowSelected property\n            row.NotifyPropertyChanged(row, e, DataGridNotificationTarget.Rows | DataGridNotificationTarget.RowHeaders);","sourceCodeStart":1036,"sourceCodeEnd":1072,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridRow.cs#L1036-L1072","documentation":"DataGridRow's IsSelected property-changed handler throws InvalidOperationException (SR.DataGridRow_CannotSelectRowWhenCells) when IsSelected is set to true on a row whose DataGrid is in DataGridSelectionUnit.Cell (or CellOnly) mode, i.e., when the row IsSelectable is false. In cell-selection mode rows themselves cannot be selected, only cells.","triggerScenarios":"Setting DataGridRow.IsSelected = true (in code, XAML, or a binding) while the owning DataGrid.SelectionUnit is DataGridSelectionUnit.Cell or CellOnly; programmatically forcing row selection on such a grid.","commonSituations":"Copying row-selection logic between grids configured with different SelectionUnit values; virtualized/recycled rows re-applying a stale IsSelected value; automation or test code selecting rows regardless of selection mode.","solutions":["Set DataGrid.SelectionUnit to FullRow (or CellOrRowHeader) to allow row selection","Select cells instead: use grid.SelectedCells and select a full row's cells when in cell mode","Guard code paths: check row.IsSelectable before setting IsSelected = true","Clear stale row IsSelected values when switching SelectionUnit at runtime"],"exampleFix":"// before\nrow.IsSelected = true; // grid is in Cell selection mode\n// after\nif (row.IsSelectable) { row.IsSelected = true; }\nelse { grid.SelectedCells.Select(new DataGridCellInfo(row.Items[0], firstColumn)); }","handlingStrategy":"type-guard","validationCode":"bool canSelectRow(System.Windows.Controls.DataGridRow row) => row.IsSelectable;","typeGuard":"bool CanSelectRow(DataGridRow row) => row?.IsSelectable == true;","tryCatchPattern":"try { row.IsSelected = true; } catch (InvalidOperationException) { /* grid is in Cell selection mode; select cells instead */ }","preventionTips":["Check DataGrid.SelectionUnit before attempting row selection","Branch selection code on row.IsSelectable","Clear stale IsSelected values when changing SelectionUnit at runtime"],"tags":["wpf","datagrid","selection","invalid-operation"],"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"}