CellSelected-CellSelectedHandler Does Not Trigger when Add record With Inline Edit
I am trying to capture the cell selected when in "Add" mode using inline edit.
What I am finding is the CellSelected grid event only triggers when editing a record (code for identifying selected record below).
How do I identify the cell being edited when adding a record so that I can calculate a field based on the cell being exited. For example Quantity field X Price field = Amount field. I want this to recalculate as they exit each field (quantity or price).
public void CellSelectedHandler(CellSelectEventArgs<tblBank> args)
{
if (args is null)
{
throw new ArgumentNullException(nameof(args));
}
Grid2CurrentCell = args.CellIndex;
}
Hi Todd,
Based on your requirements, we would like to clarify that by default, after adding data to the data grid, the cell is not selected, so the cell-selected event is not triggered. While editing the cell, the selected event is triggered because before you edit the cell, you must first select it. this is a default behavior. If you want to select the cell after adding the data, we suggest using programmatically selecting the cell.
Additionally, if you want to perform any action after the save operation is
completed in the grid, we suggest using the Request Type "Save" in
the OnActionComplete event. Here, you can obtain the added data. Please refer
to the code snippet and user guide below for your reference.
|
@using Syncfusion.Blazor.Grids
<SfGrid DataSource="@Orders" @ref="Grid" TValue="Order" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })"> <GridEvents CellSelecting="CellSelectingHandler" OnActionBegin="ActionBegin" OnActionComplete="ActionComplete" TValue="Order"></GridEvents> <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings> <GridSelectionSettings Mode=SelectionMode.Cell></GridSelectionSettings>
<GridColumns> <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn> <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn> <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> </GridColumns> </SfGrid>
@code { public List<Order> Orders { get; set; }
SfGrid<Order> Grid { get; set; }
protected override void OnInitialized() { Orders = Enumerable.Range(1, 75).Select(x => new Order() { OrderID = 1000 + x, CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)], Freight = 2.1 * x, OrderDate = DateTime.Now.AddDays(-x), }).ToList(); }
public class Order { public int? OrderID { get; set; } public string CustomerID { get; set; } public DateTime? OrderDate { get; set; } public double? Freight { get; set; } }
public void ActionBegin(ActionEventArgs<Order> args) { if (args.RequestType == Syncfusion.Blazor.Grids.Action.BeginEdit) { // Triggers before editing operation starts } else if (args.RequestType == Syncfusion.Blazor.Grids.Action.Add) { // Triggers before add operation starts } else if (args.RequestType == Syncfusion.Blazor.Grids.Action.Cancel) { // Triggers before cancel operation starts } else if (args.RequestType == Syncfusion.Blazor.Grids.Action.Save) { // Triggers before save operation starts } else if (args.RequestType == Syncfusion.Blazor.Grids.Action.Delete) { // Triggers before delete operation starts } }
public async Task ActionComplete(ActionEventArgs<Order> args) { if (args.RequestType == Syncfusion.Blazor.Grids.Action.BeginEdit) { // Triggers once editing operation completes } else if (args.RequestType == Syncfusion.Blazor.Grids.Action.Add) { // Triggers once add operation completes } else if (args.RequestType == Syncfusion.Blazor.Grids.Action.Cancel) { // Triggers once cancel operation completes } else if (args.RequestType == Syncfusion.Blazor.Grids.Action.Save) { int rowIndex = Grid.GetRowIndexByPrimaryKeyAsync(args.Data.OrderID).Result; await Grid.SelectCellAsync((rowIndex, 0)); } else if (args.RequestType == Syncfusion.Blazor.Grids.Action.Delete) { // Triggers once delete operation completes } }
public void CellSelectingHandler(CellSelectingEventArgs<Order> args) { // Here, you can customize your code. } } |
Reference:
https://blazor.syncfusion.com/documentation/datagrid/editing#event-trace-while-editing
https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_GetRowIndexByPrimaryKeyAsync_System_Object_
https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_SelectCellAsync_System_ValueTuple_System_Int32_System_Int32__System_Nullable_System_Boolean__
Regards,
Prathap S
I am trying to access the cell value when adding a record but before saving the record. For example I have 2 columns in a table and adding a new record. When I click the add button, I am in add mode. When I enter a value in column 1 and tab to column 2, I would like to access what i entered in column 1 before saving the record.
I am using inline edit mode. I understand if I was using batch edit mode I could use "OnCellSave" but I do not see an equivalent event when using inline edit.
Todd
Hi Todd,
Query: “I am trying to access the cell value when adding a record but before saving the record.”
We would like to that it is not possible to access the value before saving the added record in DataGrid. Because we can get the value once the record is inserted in DataGrid. So it is not possible to achieve your requirement.
Please let us know if you have any concerns.
Regards,
Monisha
- 3 Replies
- 3 Participants
-
TO Todd
- Nov 28, 2023 09:17 PM UTC
- Dec 8, 2023 11:27 AM UTC