Skip to main content
A newer version of this page is available. .

DxGrid.SaveChangesAsync() Method

Saves changes made in the edit form or edit row.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v23.1.dll

NuGet Package: DevExpress.Blazor

Declaration

public Task SaveChangesAsync()

Returns

Type Description
Task

The task that is completed when changes are saved in the edit form/row.

Remarks

Users can employ an inline edit row or invoke a standard or pop-up edit form to edit data in the Grid. The EditMode property specifies the current edit mode.

The EditFormTemplate and CellEditTemplate allow you to define edit form and edit row content. The default edit form/row shows the predefined Save and Cancel buttons.

To hide these buttons, use the following API depending on the current edit mode:

You can also implement your custom Save button inside or outside the Grid. Handle the button’s click event and call the SaveChangesAsync method to save changes made in the edit form/row. This method raises the EditModelSaving event. In its event handler, implement logic that applies changes to the data source.

The following example hides the predefined Save and Cancel buttons and implements custom buttons within the edit row:

Blazor Grid Edit Row with Custom Buttons

@inject NwindDataService NwindDataService

<DxGrid @ref="MyGrid"
        PageSize="5"
        Data="GridDataSource"
        EditModelSaving="OnEditModelSaving"
        DataItemDeleting="OnDataItemDeleting"
        KeyFieldName="EmployeeId"
        EditMode="GridEditMode.EditRow" >
    <Columns>
        <DxGridCommandColumn Width="200px" CancelButtonVisible="false" SaveButtonVisible="false">
            <CellEditTemplate>
                <DxButton Click="@(() => MyGrid.SaveChangesAsync())" Text="Update" />
                <DxButton Click="@(() => MyGrid.CancelEditAsync())" Text="Discard" />
            </CellEditTemplate>
        </DxGridCommandColumn>
        <DxGridDataColumn FieldName="FirstName" />
        <DxGridDataColumn FieldName="LastName" />
        <DxGridDataColumn FieldName="Title" />
        <DxGridDataColumn FieldName="HireDate" />
    </Columns>
</DxGrid>

@code {
    IEnumerable<object> GridDataSource { get; set; }
    NorthwindContext Northwind { get; set; }
    IGrid MyGrid { get; set; }

    protected override async Task OnInitializedAsync() {
        Northwind = NorthwindContextFactory.CreateDbContext();
        GridDataSource = await Northwind.Employees.ToListAsync();
    }

    async Task OnEditModelSaving(GridEditModelSavingEventArgs e) {
        var editModel = (Employee)e.EditModel;
        // Re-query a data item from the database.
        var dataItem = e.IsNew ? new Employee() : Northwind.Employees.Find(editModel.EmployeeId);
        // Assign changes from the edit model to the data item.
        if (dataItem != null) {
            dataItem.FirstName = editModel.FirstName;
            dataItem.LastName = editModel.LastName;
            dataItem.Title = editModel.Title;
            dataItem.HireDate = editModel.HireDate;
            // Post changes to the database.
            if (e.IsNew)
                await Northwind.AddAsync(dataItem);
            await Northwind.SaveChangesAsync();
            // Reload the entire Grid.
            GridDataSource = await Northwind.Employees.ToListAsync();
        }
    }

    async Task OnDataItemDeleting(GridDataItemDeletingEventArgs e) {
        // Re-query a data item from the database.
        var dataItem = Northwind.Employees.Find((e.DataItem as Employee).EmployeeId);
        if (dataItem != null) {
            // Remove the data item from the database.
            Northwind.Remove(dataItem);
            await Northwind.SaveChangesAsync();
            // Reload the entire Grid.
            GridDataSource = await Northwind.Employees.ToListAsync();
        }
    }
}

Implements

See Also