# Editing and Saving All Grid Rows

## Environment

<table>
    <tbody>
	    <tr>
	    	<td>Product</td>
	    	<td>Progress® Kendo UI for Angular Grid</td>
	    </tr>
    </tbody>
</table>

## Description

How can I enable the edit mode of all Grid rows on the current page and save the changes for all edited rows?

This Knowledge Base article also answers the following questions:

* How to batch or bulk edit all Grid rows?
* How to save the changes for all edited rows in the Grid?
* How to store the edited rows in a `FormArray`?

## Solution

The following solution relies on [`<kendo-toolbar-button>`](https://www.telerik.com/kendo-angular-ui/components/toolbar/api/toolbarbuttoncomponent) tools, rendered in the Grid toolbar. When pressed, they invoke the respective Grid methods in order to achieve multi rows editing.

> The ToolBar buttons and the logic implemented in their corresponding click handlers can be further modified to meet specific project requirements.

> The implementation relies on iterating the Grid data and might cause performance issues if a lot of Grid rows are rendered at the same time. You can use [paging](https://www.telerik.com/kendo-angular-ui/components/grid/paging/basics) or [virtual scrolling](https://www.telerik.com/kendo-angular-ui/components/grid/scroll-modes/virtual) instead.

### Editing All Rows

To put all rendered Grid rows in edit mode, iterate over the grid data and invoke the built in [`editRow`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#editrow) method for each row. The method requires a row index as first argument, and optionally a [`FormGroup`](https://angular.dev/api/forms/FormGroup) associated with each row as a second argument:

```ts
public updateGroups(grid): void {
    let rows = grid.data;

    (this.formGroups.get('items') as FormArray).clear();
    rows.forEach((_item, index) => {
        let group = this.createFormGroup(rows[index]);
        (this.formGroups.get('items') as FormArray).push(group);
        if (this.isEdited && !this.isNew) {
            grid.editRow(index, group);
        }
    });
}
```

Store the created [`FormGroup`](https://angular.dev/api/forms/FormGroup) in a [FormArray](https://angular.dev/api/forms/FormArray#) so that the respective form groups can be easily reused when necessary:

```ts
let group = this.createFormGroup(rows[index]);
(this.formGroups.get('items') as FormArray).push(group);
```

### Saving the Changes

When you want to persist the changes, iterate over the Grid data again and find the corresponding items from the created [FormArray](https://angular.dev/api/forms/FormArray#). Then you can save the edited and newly added rows by using a custom service method.

You can use the [`closeRow`](https://www.telerik.com/kendo-angular-ui/components/grid/api/gridcomponent#closeRow) method to exit the edit mode:

```ts
public saveChanges(grid: GridComponent): void {
    let rows: any = grid.data;

    rows.forEach((_item, index) => {
        const product = (this.formGroups.get('items') as FormArray).controls[index];

        this.service.save(product.value, false);
         if (saveNew) {
             grid.closeRow(index);
        }
    });
}
```

You can use the `save` service method to update the Grid collection as necessary.

```ts
public save(product: any, isNew: boolean): void {
    if (isNew) {
        this.data.unshift(product);
    } else {
        Object.assign(
            this.data.find(({ ProductID }) => ProductID === product.ProductID),
            product
        );
    }
}
```

### Adding New Rows

You can add new rows by creating a form group for the respective row and passing it to the `save` service method:

```ts
public addRowHandler(grid: GridComponent): void {
    this.formGroup = this.createFormGroup({
      ProductID: this.counter++,
      ProductName: '',
      UnitsInStock: '',
      CategoryID: null,
    });

    this.service.save(this.formGroup.value, true);
    (this.formGroups.get('items') as FormArray).insert(0, formGroup);
    ...
}
```

The following example demonstrates the full implementation of the suggested approach:

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/knowledge-base/edit-all-grid-rows/source.txt)

## See Also

* [Grid Editing Basics](https://www.telerik.com/kendo-angular-ui/components/grid/editing/basics)
* [Grid Custom Editors](https://www.telerik.com/kendo-angular-ui/components/grid/editing/custom-editors)
* [Grid External Editing](https://www.telerik.com/kendo-angular-ui/components/grid/editing/external-editing)
* [Grid In-Cell Editing](https://www.telerik.com/kendo-angular-ui/components/grid/editing/in-cell-editing)
* [Grid Inline Editing](https://www.telerik.com/kendo-angular-ui/components/grid/editing/inline-editing)
