{"record":{"id":"6065c197ae5edf17","repo":"dotnet/wpf","slug":"sr-inavalidstartitem-datagriditemautomationpeer","errorCode":null,"errorMessage":"SR.InavalidStartItem","messagePattern":"SR\\.InavalidStartItem","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridItemAutomationPeer.cs","lineNumber":157,"sourceCode":"\n            IList<DataGridColumn> columns = OwningDataGrid.Columns;\n\n            if (columns != null && columns.Count > 0)\n            {\n                DataGridCellItemAutomationPeer startAfterItem = null;\n                if (startAfter != null)\n                {\n                    // get the peer corresponding to this provider\n                    startAfterItem = PeerFromProvider(startAfter) as DataGridCellItemAutomationPeer;\n                }\n\n                // startIndex refers to the index of the item just after startAfterItem\n                int startIndex = 0;\n                if (startAfterItem != null)\n                {\n                    if (startAfterItem.Column == null)\n                    {\n                        throw new InvalidOperationException(SR.InavalidStartItem);\n                    }\n\n                    // To find the index of the item in items collection which occurs\n                    // immidiately after startAfterItem.Item\n                    startIndex = columns.IndexOf(startAfterItem.Column) + 1;\n                    if (startIndex == 0 || startIndex == columns.Count)\n                        return null;\n                }\n\n                if (propertyId == 0 && startIndex < columns.Count)\n                {\n                    return (ProviderFromPeer(GetOrCreateCellItemPeer(columns[startIndex])));\n                }\n\n                DataGridCellItemAutomationPeer currentItemPeer;\n                object currentValue = null;\n                for (int i = startIndex; i < columns.Count; i++)\n                {","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridItemAutomationPeer.cs#L139-L175","documentation":"DataGridItemAutomationPeer.FindItemByProperty throws InvalidOperationException when startAfterItem is provided but its Column is null, so the peer cannot determine the index in the columns collection at which to start searching. Only column-header peers with a live column can act as the anchor.","triggerScenarios":"Calling FindItemByProperty on a DataGridItemPeer (row) passing a startAfterItem peer whose Column property is null — e.g. a peer for a removed/virtualized column or a non-column peer passed by mistake.","commonSituations":"Stale UIA anchors after columns are regenerated; mixing row peers and column peers as anchors; virtualization dropping the underlying column reference.","solutions":["Verify startAfterItem.Column != null before using it as an anchor.","Refresh the anchor peer from a fresh FindItemByProperty/GetChildren call after any column change.","Pass null as startAfterItem to search from the beginning.","Catch InvalidOperationException and restart the search without an anchor."],"exampleFix":"// before\nvar found = rowPeer.FindItemByProperty(badAnchor, 0, null); // badAnchor.Column == null\n// after\nvar found = (badAnchor?.Column != null)\n    ? rowPeer.FindItemByProperty(badAnchor, 0, null)\n    : rowPeer.FindItemByProperty(null, 0, null);","handlingStrategy":"validation","validationCode":"if (anchor != null && anchor.Column == null)\n    anchor = null; // restart search from beginning","typeGuard":"bool HasLiveColumn(AutomationPeer p) => p is DataGridColumnHeaderAutomationPeer ch && ch.Column != null;","tryCatchPattern":"try { return peer.FindItemByProperty(anchor, 0, value); }\ncatch (InvalidOperationException) { return peer.FindItemByProperty(null, 0, value); }","preventionTips":["Use null anchors when column state may have changed recently","Re-fetch anchors after column add/remove/reorder operations","Do not mix row peers and column-header peers as anchors"],"tags":["wpf","uiautomation","datagrid","automation-peer"],"backgroundTag":"null-argument","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"}