Blazorise DataGrid: Editing
The DataGrid component allows you to dynamically insert, delete, and update records.
Overview
The grid can perform some basic CRUD operations on the supplied Data
collection. To enable editing on data-grid, set the Editable
attribute to true on the DataGrid, and then set Editable
to true on each column you wish to be editable.
By default every time the Item
is saved it will be automatically handled by the data-grid itself. That means that all its fields will be populated after the user clicks on Save button. If you want to change that, you can just disable it by setting the UseInternalEditing
to false.
Edit Modes
The grid can work in several different editing modes that can provide different user experiences.
EditMode
:
Form
editing is done in the internal DataGrid formInline
editing is done in the current rowPopup
editing is done in the the modal dialogCell
any cell value can be edited directly allowing for rapid editing of data.
<Field> <FieldLabel> Edit Mode </FieldLabel> <FieldBody> <Select @bind-SelectedValue=""> <SelectItem Value="DataGridEditMode.Form">Form</SelectItem> <SelectItem Value="DataGridEditMode.Inline">Inline</SelectItem> <SelectItem Value="DataGridEditMode.Popup">Popup</SelectItem> <SelectItem Value="DataGridEditMode.Cell">Cell ("Rapid Editing")</SelectItem> </Select> </FieldBody> </Field> <DataGrid TItem="Employee" Data="" @bind-SelectedRow="" Editable Responsive ShowPager CommandMode="DataGridCommandMode.ButtonRow" EditMode="editMode"> <DataGridColumns> <DataGridCommandColumn NewCommandAllowed="false" EditCommandAllowed="false" DeleteCommandAllowed="false" CancelCommandAllowed > <SaveCommandTemplate> <Button ElementId="btnSave" Type="ButtonType.Submit" PreventDefaultOnSubmit Color="Color.Primary" Clicked="@context.Clicked">@context.LocalizationString</Button> </SaveCommandTemplate> <CancelCommandTemplate> <Button ElementId="btnCancel" Color="Color.Secondary" Clicked="@context.Clicked">@context.LocalizationString</Button> </CancelCommandTemplate> </DataGridCommandColumn> <DataGridColumn Field="@nameof(Employee.Id)" Caption="#" Sortable="false" /> <DataGridColumn Field="@nameof(Employee.FirstName)" Caption="First Name" Editable /> <DataGridColumn Field="@nameof(Employee.LastName)" Caption="Last Name" Editable /> <DataGridColumn Field="@nameof(Employee.Email)" Caption="Email" Editable /> <DataGridNumericColumn Field="@nameof(Employee.Salary)" Caption="Salary" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" Editable /> </DataGridColumns> <ButtonRowTemplate> <Button Color="Color.Success" Clicked="context.NewCommand.Clicked">New</Button> <Button Color="Color.Primary" Disabled="(selectedEmployee is null)" Clicked="context.EditCommand.Clicked">Edit</Button> <Button Color="Color.Danger" Disabled="(selectedEmployee is null)" Clicked="context.DeleteCommand.Clicked">Delete</Button> <Button Color="Color.Link" Clicked="context.ClearFilterCommand.Clicked">Clear Filter</Button> </ButtonRowTemplate> </DataGrid>
@code{ [Inject] public EmployeeData EmployeeData { get; set; } private List<Employee> employeeList; private Employee selectedEmployee; private DataGridEditMode editMode = DataGridEditMode.Form; protected override async Task OnInitializedAsync() { employeeList = await EmployeeData.GetDataAsync(); await base.OnInitializedAsync(); } }
Batch Edit
The grid can be set to batch edit mode by setting BatchEdit
to true. This allows the user to edit multiple rows and then save all the changes at once.
<Field> <FieldLabel> Edit Mode </FieldLabel> <FieldBody> <Select @bind-SelectedValue=""> <SelectItem Value="DataGridEditMode.Form">Form</SelectItem> <SelectItem Value="DataGridEditMode.Inline">Inline</SelectItem> <SelectItem Value="DataGridEditMode.Popup">Popup</SelectItem> <SelectItem Value="DataGridEditMode.Cell">Cell ("Rapid Editing")</SelectItem> </Select> </FieldBody> </Field> <DataGrid @ref=dataGridRef TItem="Employee" Data="inMemoryData" Responsive ShowPager ShowPageSizes @bind-SelectedRow="" Editable EditMode="" BatchEdit BatchChange="OnBatchChange" BatchSaving="OnBatchSaving" BatchSaved="OnBatchSaved" UseValidation ValidationsSummaryLabel="The following validation errors have occurred..." CommandMode="DataGridCommandMode.ButtonRow" ShowValidationsSummary> <DataGridColumns> <DataGridCommandColumn SaveBatchCommandAllowed=false CancelBatchCommandAllowed=false /> <DataGridColumn TextAlignment="TextAlignment.Center" TItem="Employee" Field="@nameof( Employee.Id )" Caption="#" Width="60px" /> <DataGridColumn TItem="Employee" Field="@nameof( Employee.FirstName )" Caption="First Name" Editable /> <DataGridColumn TItem="Employee" Field="@nameof( Employee.LastName )" Caption="Last Name" Editable /> <DataGridColumn TItem="Employee" Field="@nameof( Employee.Email )" Caption="Email" Editable /> <DataGridColumn TItem="Employee" Field="@nameof( Employee.Salary )" Caption="Salary" Editable Width="140px" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" TextAlignment="TextAlignment.End" /> </DataGridColumns> <ButtonRowTemplate> <Button Color="Color.Success" Clicked="context.NewCommand.Clicked">New</Button> <Button Color="Color.Primary" Disabled="(selectedEmployee is null)" Clicked="context.EditCommand.Clicked">Edit</Button> <Button Color="Color.Danger" Disabled="(selectedEmployee is null)" Clicked="context.DeleteCommand.Clicked">Delete</Button> <Button Color="Color.Link" Clicked="context.ClearFilterCommand.Clicked">Clear Filter</Button> <Button Color="Color.Success" Disabled="(batchQuantity == 0)" Clicked="@(context.SaveBatchCommand.Clicked)">@context.SaveBatchCommand.LocalizationString</Button> <Button Color="Color.Default" Clicked="@(context.CancelBatchCommand.Clicked)">@context.CancelBatchCommand.LocalizationString</Button> </ButtonRowTemplate> </DataGrid>
@code { [Inject] EmployeeData EmployeeData { get; set; } private int batchQuantity = 0; private DataGrid<Employee> dataGridRef; private List<Employee> inMemoryData; private Employee selectedEmployee; private DataGridEditMode editMode = DataGridEditMode.Form; protected override async Task OnInitializedAsync() { inMemoryData = ( await EmployeeData.GetDataAsync().ConfigureAwait( false ) ).Take( 25 ).ToList(); await base.OnInitializedAsync(); } private Task OnBatchChange( DataGridBatchChangeEventArgs<Employee> args ) { Console.WriteLine( "Batch Change" ); batchQuantity = dataGridRef.BatchChanges.Count; return Task.CompletedTask; } private Task OnBatchSaving( DataGridBatchSavingEventArgs<Employee> args ) { Console.WriteLine( "Batch Saving" ); return Task.CompletedTask; } private Task OnBatchSaved( DataGridBatchSavedEventArgs<Employee> args ) { Console.WriteLine( "Batch Saved" ); batchQuantity = 0; return Task.CompletedTask; } }
NewItemDefaultSetter
NewItemDefaultSetter
function is used to set default values when new item is created and before the edit form is shown. It will only be evaluate, if DataGrid
is editable.
<DataGrid TItem="Employee" Data="" @bind-SelectedRow="" NewItemDefaultSetter="" Editable Responsive ShowPager> <DataGridCommandColumn /> <DataGridColumn Field="@nameof(Employee.Id)" Caption="#" Sortable="false" /> <DataGridColumn Field="@nameof(Employee.FirstName)" Caption="First Name" Editable /> <DataGridColumn Field="@nameof(Employee.LastName)" Caption="Last Name" Editable /> <DataGridColumn Field="@nameof(Employee.Email)" Caption="Email" Editable /> <DataGridColumn Field="@nameof(Employee.Salary)" Caption="Salary" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" Editable> <EditTemplate> <NumericEdit TValue="decimal" Value="@((decimal)context.CellValue)" ValueChanged="@( v => context.CellValue = v)" /> </EditTemplate> </DataGridColumn> </DataGrid>
@code{ [Inject] public EmployeeData EmployeeData { get; set; } private List<Employee> employeeList; private Employee selectedEmployee; protected override async Task OnInitializedAsync() { employeeList = await EmployeeData.GetDataAsync(); await base.OnInitializedAsync(); } void OnEmployeeNewItemDefaultSetter( Employee employee ) { employee.Salary = 100.0M; employee.IsActive = true; } }
Cascading values
In some case you want to update a different cell in a DataGrid when you update a value. This can be achieved with an UpdateCell
method. You have two ways of updating a cell:
- by calling
UpdateCell
on the context inside ofEditTemplate
, or - by calling
UpdateCellEditValue
on theDataGrid
instance
In the following example we’re simply calling context.UpdateCell
with a field-name to change and a new value that we want it to assign:
<DataGrid TItem="Employee" Data="" Editable EditMode="DataGridEditMode.Inline" Responsive ShowPager> <DataGridCommandColumn /> <DataGridColumn Field="@nameof( Employee.Salary )" Caption="Salary" Editable DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")"> <EditTemplate> <NumericEdit TValue="decimal" Value="@((decimal)context.CellValue)" ValueChanged="@( v => { context.CellValue = v; context.UpdateCell( nameof( Employee.Tax ), v * context.Item.TaxPercentage ); })" /> </EditTemplate> </DataGridColumn> <DataGridColumn Field="@nameof( Employee.Tax )" Caption="Tax" Editable DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")"> <EditTemplate> <NumericEdit TValue="decimal" Value="@((decimal)context.CellValue)" Disabled /> </EditTemplate> </DataGridColumn> </DataGrid>
@code { [Inject] public EmployeeData EmployeeData { get; set; } private List<Employee> employeeList; protected override async Task OnInitializedAsync() { employeeList = await EmployeeData.GetDataAsync(); await base.OnInitializedAsync(); } }
API
See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.