dotnet/wpf · error · InvalidOperationException

SR.DataGrid_AutomationInvokeFailed

Error message

SR.DataGrid_AutomationInvokeFailed

What it means

DataGridItemAutomationPeer.Invoke throws InvalidOperationException when invoking the row neither committed a new item nor the row is not a NewItemPlaceholder. I.e., the DataGrid's BeginEdit/commit attempt on the row failed and the row is an ordinary row, so automation Invoke semantics could not be fulfilled.

Solutions

  1. Verify the DataGrid is not read-only (DataGrid.IsReadOnly == false) and the row supports editing before invoking.
  2. Ensure row validation rules pass so BeginEdit/commit succeeds.
  3. For placeholder rows, check that CanUserAddRows is enabled.
  4. Catch InvalidOperationException around the Invoke call and handle non-editable rows explicitly.

Example fix

// before
invokePattern.Invoke(); // throws on read-only grid
// after
if (!dataGrid.IsReadOnly && dataGrid.CanUserAddRows)
    invokePattern.Invoke();
Defensive patterns

Strategy: try-catch

Validate before calling

bool canInvoke = !dataGrid.IsReadOnly && (rowPlaceholder ? dataGrid.CanUserAddRows : true);

Try / catch

try { invokePattern.Invoke(); }
catch (InvalidOperationException) { /* row is not editable; use keyboard/DataGrid API instead */ }

Prevention

When it happens

Trigger: Calling the UIA Invoke pattern on a DataGrid row when DataGrid.BeginEdit (or the placeholder's commit) returns false — e.g. the row is read-only, editing is disabled, or a commit validation error blocks it.

Common situations: Test automation clicking rows in a read-only DataGrid (IsReadOnly=true) or with no editing commands; validation rules rejecting the commit of a NewItemPlaceholder row.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a975999050a9bdc2. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridItemAutomationPeer.cs:251

                {
                    if (this.OwningDataGrid.Columns.Count > 0)
                    {
                        DataGridCell cell = this.OwningDataGrid.TryFindCell(item, this.OwningDataGrid.Columns[0]);
                        if (cell != null)
                        {
                            this.OwningDataGrid.UnselectAll();
                            cell.Focus();
                            success = this.OwningDataGrid.BeginEdit();
                        }
                    }
                }
            }

            // Invoke on a NewItemPlaceholder row creates a new item.
            // BeginEdit on a NewItemPlaceholder row returns false.
            if (!success && !IsNewItemPlaceholder)
            {
                throw new InvalidOperationException(SR.DataGrid_AutomationInvokeFailed);
            }
        }

        #endregion

        #region IScrollItemProvider

        void IScrollItemProvider.ScrollIntoView()
        {
            this.OwningDataGrid.ScrollIntoView(Item);
        }

        #endregion

        #region ISelectionItemProvider

        bool ISelectionItemProvider.IsSelected
        {

View on GitHub (pinned to 81131a70a4)