In a Blazor DataGrid with inline edit mode, a new row show up on INSERT key press. How to prevent that?

Answer:


By default, that support is provided to add new record on pressing INSERT key in Grid. The ToolbarClick and OnBatchAdd events need to be canceled to prevent showing the new record row. Here is the code snippet for your reference,


<SfGrid DataSource="@Orders" AllowPaging="true" Toolbar="@(new List() { "Add", "Edit", "Delete", "Cancel", "Update" })" Height="315">
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch">GridEditSettings>
<GridEvents TValue="Order" OnToolbarClick="toolbarClick" OnBatchAdd="BeforeAdd">GridEvents>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" ValidationRules="@(new ValidationRules { Required = true })" TextAlign="TextAlign.Right" Width="120">GridColumn>
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" ValidationRules="@(new ValidationRules { Required = true })" Width="120">GridColumn>
. . . . . . . . .
GridColumns>
SfGrid>

@code{
public List Orders { get; set; }
private bool PreventUpdate { get; set; }

public void BeforeAdd(BeforeBatchAddArgs<Order> args)
{
if (!PreventUpdate)
{
args.Cancel = true; // prevent the add operation
}
PreventUpdate = false;
}

public void toolbarClick(Syncfusion.Blazor.Navigations.ClickEventArgs args)
{
if (args.Item.Text == "Add") // if toolbar as add then we set variable as true to perform add action
{
PreventUpdate = true;
}
}
}



Loader.
Up arrow icon