# Batch Editing in a Blazor Grid

URL: https://flexcoreui.com/demo/grid/batch-editing  
Section: Data Grid  
Controls: GridControl, GridColumn, GridEditSettings  
Keywords: batch editing, cell editing, in-cell editing, inline editing, grid editing, validation

Edit a Blazor grid like a spreadsheet: double-click a cell on a 5,000-line rate card, type, and Enter or Tab writes the value to your object.

## Documentation

### Grid

| Name | Type | Default | Description |
|---|---|---|---|
| EditSettingsRef | EditSettings? | null | The settings object below. Null leaves every cell read-only: each way into an editor first checks AllowEditing on it. |
| EventsRef | GridControlEvents<TValue>? | null | The object that holds the callbacks below; the grid has no separate parameters for the edit events. |
| BatchEditBehavior | GridBatchEditBehavior | GridBatchEditBehavior.MultiRow | Picks one of the two batch behaviors in the table below. |
| EditOnSingleClick | bool | false | A plain click on an editable batch cell opens its editor at once, instead of only selecting the cell. |
| CellEditablePredicate | Func<TValue, string, bool>? | null | Per-row veto, called with the row and the field before a cell opens and again for each row a multi-row commit fans out to. A false answer leaves that cell read-only without raising OnCellEdit. |
| NewItemFactory | Func<TValue>? | null | Builds the row for AppendRow, InsertRow and the Inline or Dialog add editor. Without it the grid calls Activator.CreateInstance<TValue>(). |
| AllowSingleCellColumnMassEdit | bool | false | With SingleCell and cell selection, Ctrl or Shift can select several cells of one column, and a typed value commits to all of them. |

### Settings

| Name | Type | Default | Description |
|---|---|---|---|
| AllowEditing | bool | false | Master switch. Double-click, Enter, F2, the click options, type-ahead commits and BeginEditCellAsync all check it first, and the editable-cell cue is drawn only while it is on. |
| AllowAdding | bool | false | Read only by the trailing-new-row features, EnsureTrailingNewRow and AddNewRowOnLastCellExit. The toolbar Add item, AppendRow and InsertRow do not consult it. |
| AllowDeleting | bool | false | Gate for the toolbar Delete item and a column's delete command, which run the confirm step and raise RowDeleting and RowDeleted. DeleteRecordAsync does not consult it. |
| Mode | EditMode | EditMode.Inline | Which editor opens; the values are in the next table. |
| ShowConfirmDialog | bool | true | A toolbar or command delete first shows a dialog saying how many records will go; false deletes at once. Not consulted for edits. |
| NewRowPosition | NewRowPosition | NewRowPosition.Top | Where the Inline add editor sits and where the saved new row goes in the list: index 0 for Top, the end for Bottom. Batch mode does not read it. |
| AllowEditOnDblClick | bool | true | In Batch mode a double-click on an editable cell opens its editor; in Inline and Dialog mode a double-click on the row opens the row editor. False leaves the double-click to OnRecordDoubleClick. |
| EditOnEnterKey | bool | true | Enter on the active editable cell opens its editor with the value selected. A cell with edit options opens its list instead and does not read this. |
| EditOnActiveCellClick | bool | false | A plain click on the cell that was already active opens its editor: the first click selects, the second edits. |

### Modes

| Name | Description |
|---|---|
| Inline | A row editor takes the row's place, or sits at NewRowPosition for an add, with a generated control per editable column or the column's EditTemplate. Enter saves, Escape cancels, and OnBeginEdit and the Row events fire around it. |
| Dialog | The same row editor inside a DialogControl titled Edit Record or Add New Record; closing the dialog cancels. |
| Batch | No row editor. One cell at a time opens in place; Enter, Tab or a click elsewhere commits it to the bound object and Escape abandons it. OnCellEdit and OnCellSave report it; OnBeginEdit and the Row events belong to the row editor. |

### Behaviors

| Name | Description |
|---|---|
| MultiRow | The default. When the edited row is one of several selected rows and the user typed in the editor, the commit writes the value to every selected row the predicate allows and raises OnCellSave once per row; OnCellEdit fired only for the cell that opened. Typing with several rows selected and no editor open starts a type-ahead that commits the same way. |
| SingleCell | A click selects a cell, a double-click edits it, and typed keys go to that one cell and commit on Enter. Several cells of one column can be edited together only through AllowSingleCellColumnMassEdit. |

### Column

| Name | Type | Default | Description |
|---|---|---|---|
| AllowEditing | bool | true | False keeps the cell read-only: batch and keyboard entry skip it, the Inline editor shows its plain value and the Dialog editor leaves it out. The example sets it on every column. |
| IsPrimaryKey | bool | false | Never editable, whatever AllowEditing says: batch entry, BeginEditCellAsync and type-ahead skip it, the Inline editor shows its plain value and the Dialog editor shows it disabled. |
| SelectAllOnEdit | bool | false | Read when the editor opens without a pointer position, for example from BeginEditCellAsync: the existing value is pre-selected so the first keystroke replaces it. Enter and F2 pre-select regardless; a click or double-click puts the caret under the pointer. |
| MaxLength | int? | null | Limit of the text editor. The browser clips a longer paste and CellValueTruncated reports it. |
| ShowNumericSpinner | bool | false | Number columns edit in a plain text editor with a decimal input mode; true renders a native number input with spinner arrows. |
| EditTemplate | RenderFragment<object>? | null | Replaces the generated control in the Inline and Dialog row editors; context is the row being edited, a copy until it is saved. The batch cell editor does not use it, and keyboard entry skips a column that has one. |
| PreferCellEditOnDoubleClick | bool | false | Declared, but GridControl never reads it. |
| ValidationRules | string? | null | Declared, but GridControl never reads it. Batch validation comes from the grid's ValidationSettings: data annotations and a custom validator. |
| DefaultValue | string? | null | Declared, but GridControl never reads it. A new row's values come from NewItemFactory. |

### Column editors

| Name | Type | Default | Description |
|---|---|---|---|
| EditOptions | IEnumerable<string>? | null | Fixed choices for an in-cell dropdown editor in Batch mode. An entry written #value;text shows the text and stores the value; a plain entry is both. |
| EditOptionsProvider | Func<object, IEnumerable<string>>? | null | Row-aware choices, called with the row and used instead of EditOptions when it returns a list; an exception from it falls back to EditOptions. |
| AllowCustomEditOptionValue | bool | false | Renders the dropdown as an editable combo that also accepts a typed value outside the list. |
| OpenEditOptionsOnEdit | bool | true | Opens the list as soon as a click or double-click starts the edit; false leaves that to the arrow button. |
| RequireEditValueInList | bool | false | A typed value must match an allowed entry, case-insensitively and on either side of a mapped option, before the cell commits; the stored value is the mapped value. A miss shows the not-found message and keeps the editor open; an empty entry always passes. The list is EditValueListProvider, else EditValueList, else the edit options. |
| EditValueList | IEnumerable<string>? | null | Allowed values for RequireEditValueInList on a cell with a picker button rather than a dropdown. |
| EditValueListProvider | Func<object, IEnumerable<string>>? | null | Row-aware allowed values; an exception from it counts as no list. |
| EditValueNotFoundMessage | string? | null | Text of the refusal; a {0} placeholder takes the refused entry. Null gives the column name, the entry and not found. |
| ShowEditButton | bool | false | Adds a trailing picker button to the cell while its editor is open. A click on it closes the editor and raises OnEditButtonClick; Enter on the active cell does the same when a handler is attached. |
| EditButtonText | string | "..." | Caption of that button. |
| AlwaysShowEditButton | bool | false | Paints the button in the display cell as well, not only while editing. |
| OpenEditButtonOnDoubleClick | bool | true | A double-click on a ShowEditButton cell raises OnEditButtonClick as the button would. |
| ShowEditButtonPredicate | Func<object, bool>? | null | Per-row veto for AlwaysShowEditButton; false hides the button on that row. |
| OpenDateCalendarOnDoubleClick | bool | false | Date columns edit as text; true adds the calendar button to the editor and opens the picker on a double-click. |

### Events

| Name | Type | Description |
|---|---|---|
| OnCellEdit | CellEditArgs<TValue> | Before a batch cell opens, and before a checkbox toggle or a context-menu paste or clear writes a cell. Set Cancel to refuse. Not raised for the further rows of a multi-row commit, nor for a cell the predicate refused. |
| OnCellSave | CellSaveArgs<TValue> | After a value is written to the bound object: when a batch editor commits with a changed value or one the user typed, once per row the commit fanned out to, and after a checkbox toggle, a paste or clear or a type-ahead commit. The example counts these. |
| OnEditButtonClick | CellEditButtonArgs<TValue> | The picker button of a ShowEditButton cell was clicked, the cell was double-clicked with OpenEditButtonOnDoubleClick, or Enter was pressed on it; an open editor on that cell is closed first. |
| CellValueTruncated | CellValueTruncatedArgs<TValue> | A paste into a text editor with MaxLength was longer than the limit. The browser already clipped it; this reports the pasted and kept lengths. |
| OnTypeAheadCommit | TypeAheadCommitArgs<TValue> | A value typed with several rows selected and no editor open is being committed. With a handler the grid passes the editable selected rows and the text and writes nothing itself; without one it runs the normal validation and OnCellSave pipeline per row. |
| OnBeginEdit | RowEditEventArgs<TValue> | Before the Inline or Dialog row editor opens on an existing row: row double-click, F2 with one row selected, the toolbar Edit item or a column's edit command. Set Cancel to keep it closed. Not raised for an add. |
| RowUpdating / RowUpdated | RowEditEventArgs<TValue> | Around saving the row editor over an existing row: RowUpdating with the edited copy before it is written back to the list item (set Cancel to keep the editor open), RowUpdated after. |
| RowCreating / RowCreated | RowEditEventArgs<TValue> | Around saving an added row: RowCreating before it is inserted into the list at NewRowPosition (cancellable), RowCreated after. |
| RowDeleting / RowDeleted | RowEditEventArgs<TValue> | Per record of a toolbar or command delete, after the confirm step: RowDeleting before the removal (set Cancel to keep that record), RowDeleted after. DeleteRecordAsync raises neither. |
| CellSaved | CellSavedArgs<TValue> | Declared, but GridControl never raises it; OnCellSave is the event that fires after a cell commits. |
| OnActionComplete | ActionEventArgs<TValue> | Declared, but GridControl never raises it. |

### Edit args

| Name | Type | Default | Description |
|---|---|---|---|
| Data | TValue |  | The row about to be edited. |
| ColumnName | string | "" | The column's Field. |
| Cancel | bool | false | Set true to keep the cell read-only this time. |

### Save args

| Name | Type | Default | Description |
|---|---|---|---|
| Data | TValue |  | The row that was written; in a multi-row commit, each selected row in turn. |
| ColumnName | string | "" | The column's Field. |
| Value | object? | null | What was written: the editor text for a commit or type-ahead, a bool for a checkbox toggle. |

### Row args

| Name | Type | Default | Description |
|---|---|---|---|
| Data | TValue? | null | The row: the list item for OnBeginEdit and the delete events, the edited copy for the update and create events. |
| RowIndex | int | 0 | The row's index in the current display order; 0 for the create events. |
| Cancel | bool | false | Read by OnBeginEdit, RowUpdating, RowCreating and RowDeleting; the completed events ignore it. |

### Methods

| Name | Type | Description |
|---|---|---|
| BeginEditCellAsync(TValue row, string field) | Task | Pages to the row if needed, selects the cell and opens its batch editor as a click would, raising OnCellEdit. Needs Batch mode and an editable, non-key column; otherwise a no-op. |
| EndEditAsync() | Task | Commits whatever is open: a pending type-ahead, the batch cell editor (validation and OnCellSave included), then a row editor. A validation failure keeps the editor open. |
| AppendRow(TValue record, string? editField = null, bool beginEdit = true) | Task<TValue> | Adds the row at the end of the list, which must be an IList<TValue> or it throws, and by default opens the editor on editField or the first editable cell. The overload without record builds one with NewItemFactory; AppendRowAsync is the same method. |
| InsertRow(int rowIndex, TValue record, string? editField = null, bool beginEdit = true) | Task<TValue> | The same at rowIndex, clamped to the list; the overload without record and InsertRowAsync as above. |
| AddRecordAsync(TValue record) | Task | Adds to the list and repaints; nothing happens when DataSource is not an IList<TValue>. No editor, no events. |
| DeleteRecordAsync(TValue record) | Task | Removes the row from the list and the selection and repaints. No confirm dialog, no RowDeleting or RowDeleted, and AllowDeleting is not consulted. |

## Examples

### Edit

```razor
@using Fx.ControlKit.Grid

<GridControl TValue="RateCardItem"
             DataSource="@_lines"
             ShowRowCountInFooter="true"
             AllowSelection="true"
             AllowSorting="true"
             AllowResizing="true"
             AllowFiltering="true"
             ShowSearchBar="true"
             Height="420px"
             EditSettingsRef="@_editSettings"
             EventsRef="@_events"
             CssClass="fx-grid-compact">
    <GridColumnsBase>
        <GridColumn Field="@nameof(RateCardItem.Code)" HeaderText="Code" Width="95px" AllowEditing="false" />
        <GridColumn Field="@nameof(RateCardItem.Description)" HeaderText="Description" Width="230px" AllowEditing="true" />
        <GridColumn Field="@nameof(RateCardItem.Region)" HeaderText="Region" Width="130px" AllowEditing="false" />
        <GridColumn Field="@nameof(RateCardItem.Destination)" HeaderText="Destination" Width="115px" AllowEditing="false" />
        <GridColumn Field="@nameof(RateCardItem.Category)" HeaderText="Category" Width="125px" AllowEditing="true" />
        <GridColumn Field="@nameof(RateCardItem.Unit)" HeaderText="Unit" Width="75px" TextAlign="TextAlign.Center" AllowEditing="true" />
        <GridColumn Field="@nameof(RateCardItem.Rate)" HeaderText="Rate" Width="100px" Type="ColumnType.Number" TextAlign="TextAlign.Right" AllowEditing="true" SelectAllOnEdit="true" Format="C2" />
        <GridColumn Field="@nameof(RateCardItem.MarginPercent)" HeaderText="Margin %" Width="90px" Type="ColumnType.Number" TextAlign="TextAlign.Right" AllowEditing="true" SelectAllOnEdit="true" />
        <GridColumn Field="@nameof(RateCardItem.Supplier)" HeaderText="Supplier" Width="200px" AllowEditing="true" />
    </GridColumnsBase>
</GridControl>
<p class="result">@_saveCount @(_saveCount == 1 ? "cell" : "cells") saved@(_last is null ? "" : $"; last: {_last}")</p>
```

```csharp
private List<RateCardItem> _lines = [];

private int _saveCount;

private string? _last;

private readonly EditSettings _editSettings = new()
{
    AllowEditing = true,
    Mode = EditMode.Batch
};

private readonly GridControlEvents<RateCardItem> _events = new();

protected override void OnInitialized()
{
    _lines = BenchData.RateCard.ToList();

    _events.OnCellSave = EventCallback.Factory.Create<CellSaveArgs<RateCardItem>>(this, args =>
    {
        _saveCount++;
        _last = $"{args.ColumnName} = {args.Value} on {args.Data.Code}";
    });
}

// Data/BenchModels.cs
public sealed class RateCardItem
{
    public string Code { get; set; } = string.Empty;
    public string Region { get; set; } = string.Empty;
    public string Destination { get; set; } = string.Empty;
    public string Category { get; set; } = string.Empty;
    public string Supplier { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
    public string Unit { get; set; } = string.Empty;
    public decimal Rate { get; set; }
    public int MarginPercent { get; set; }
    public string Season { get; set; } = string.Empty;
    public bool Bookable { get; set; } = true;
    public string Notes { get; set; } = string.Empty;
}
```

