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 dialog
<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> </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" > <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 /> <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> </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(); } }
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.