dotnet/wpf · error · InvalidOperationException

SR.DataGrid_ColumnIsReadOnly

Error message

SR.DataGrid_ColumnIsReadOnly

What it means

DataGridCellItemAutomationPeer's IValueProvider.SetValue sets a cell's value via OwningDataGrid.SetCellAutomationValue. If the column is read-only (DataGridColumn.IsReadOnly == true), the value cannot be written, so the peer throws InvalidOperationException with SR.DataGrid_ColumnIsReadOnly before attempting the set.

Solutions

  1. Check column.IsReadOnly before calling SetValue and skip or use a different editable column.
  2. Make the column editable if the business scenario requires programmatic updates (set IsReadOnly=false or remove the read-only binding).
  3. Catch InvalidOperationException and surface a clear message that the column is not editable.
  4. Update test data to only target writable columns.

Example fix

// before
((IValueProvider)cellPattern).SetValue("42");

// after
if (!column.IsReadOnly)
    ((IValueProvider)cellPattern).SetValue("42");
Defensive patterns

Strategy: validation

Validate before calling

if (!column.IsReadOnly && !dataGrid.IsReadOnly) valueProvider.SetValue(value);

Type guard

bool IsWritable(DataGridColumn c, DataGrid g) => c != null && !c.IsReadOnly && g != null && !g.IsReadOnly;

Try / catch

try { valueProvider.SetValue(value); } catch (InvalidOperationException ex) { /* surface read-only message */ }

Prevention

When it happens

Trigger: An automation client calls SetValue(string) (IValueProvider) on a cell whose DataGridColumn.IsReadOnly is true — including columns made read-only via binding, DataGrid.IsReadOnly, or read-only row (NewItemPlaceholder).

Common situations: UIA data-entry tests against DataGrids where some columns are intentionally read-only (e.g. computed or key columns); automated form fillers; apps that toggle IsReadOnly at runtime based on permissions.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Automation/Peers/DataGridCellItemAutomationPeer.cs:753

        }

        #endregion

        #region IValueProvider

        bool IValueProvider.IsReadOnly
        {
            get
            {
                return _column.IsReadOnly;
            }
        }

        void IValueProvider.SetValue(string value)
        {
            if (_column.IsReadOnly)
            {
                throw new InvalidOperationException(SR.DataGrid_ColumnIsReadOnly);
            }
            if (this.OwningDataGrid != null)
            {
                OwningDataGrid.SetCellAutomationValue(Item, _column, value);
            }
        }

        string IValueProvider.Value
        {
            get
            {
                if (this.OwningDataGrid != null)
                {
                    return OwningDataGrid.GetCellAutomationValue(Item, _column);
                }
                else
                {
                    return null;

View on GitHub (pinned to 81131a70a4)