Hi,
I have a some problems with a grid in batch mode in my Blazor Server application.
When I add new records all new records get green, which, I think, means "not updated". They should change to white due to OnCellSave.
When deleting added records I often get exceptions.
What is wrong in my code?
Regards, Bruno
My Code: ======================================================
<SfGrid @ref="Grid" DataSource="@TimeSeries!.Points" Row AllowPaging="true" AllowSelection="true"
Toolbar="@(new List<string>() { "Add", "Delete", "Cancel"})" Height ="315">
<GridSelectionSettings Mode="Syncfusion.Blazor.Grids.SelectionMode.Row" Type="Syncfusion.Blazor.Grids.SelectionType.Multiple"></GridSelectionSettings>
<GridEditSettings Mode="EditMode.Batch" AllowAdding="true" AllowEditing="true" AllowDeleting="true"
ShowConfirmDialog="false" NewRowPosition="NewRowPosition.Bottom"></GridEditSettings>
<GridEvents OnToolbarClick="OnToolbarClick" DataBound="OnDataBound" CellSaved="OnCellSave" TValue="TimePoint"></GridEvents>
<GridColumns>
<GridColumn Width="20" Type="ColumnType.Number" AllowFiltering="false" AllowEditing="false" AllowSorting="false"
TextAlign="Syncfusion.Blazor.Grids.TextAlign.Center" AutoFit="true">
<Template>
@{
var timePoint = context as TimePoint;
var rowIndex = -1;
if (timePoint != null)
rowIndex = TimeSeries.Points.IndexOf(timePoint) + 1;
}
@rowIndex
</Template>
</GridColumn>
<GridColumn Field=@nameof(TimePoint.Time) HeaderText="Time" TextAlign="TextAlign.Right" Width="120" EditorSettings="@EditorSettings"></GridColumn>
<GridColumn Field=@nameof(TimePoint.Value) HeaderText="Value" TextAlign="TextAlign.Right" Width="120" EditorSettings="@EditorSettings"></GridColumn>
</GridColumns>
</SfGrid>
@code {
[Parameter]
public TimeSeries? TimeSeries { get; set; }
SfGrid<TimePoint>? Grid { get; set; }
IEditorSettings EditorSettings = new NumericEditCellParams
{
Params = new NumericTextBoxModel<object>() { ShowSpinButton = false }
};
async Task OnCellSave(CellSaveArgs<TimePoint> e)
{
if (e.ColumnName != "") // prevent exception
{
await Grid!.EndEditAsync();
}
}
async Task OnToolbarClick(Syncfusion.Blazor.Navigations.ClickEventArgs args)
{
// Prevent "no row selected" dialog box
if (args.Item.Text == "Delete")
{
if (Grid is not null)
{
var selRows = await Grid.GetSelectedRecordsAsync();
args.Cancel = selRows == null || selRows.Count == 0;
}
}
}
}
Hi Bruno,
Greetings from Syncfusion support.
We could see that in your shared code snippet you have not enabled IsPrimaryKey property. We would like to inform you that CRUD operation in Grid will take place only based on the unique PrimaryKey column value. So IsPrimaryKey property must be enabled to any one of the unique valued column defined in grid. Only based on primary key value, the CRUD will be properly performed in Grid. So kindly check the reported issue after using IsPrimaryKey for an unique column.
Reference : https://blazor.syncfusion.com/documentation/datagrid/editing
Kindly get back to us if you still face difficulties or if you have further queries at your end.
Regards,
Naveen Palanivel
Hi Naveen
The lack of IsPrimaryKey was one reason. But there is also a second problem I cannot resolve.
I tried out two implementation of my data model:
1.) Standard case: >>> works!
public class TimePoint
{
public int Id { get; set; }
public double Time { get; set; }
public double Value { get; set; }
}
List<TimePoint> TimeSeries = new List<TimePoint>();
DataSource="@TimeSeries"
2.) Model is derived from IList >>> does not work!
public class TimeSeries : IList<TimePoint>
{
// implementation of all IList methods
}
public TimeSeries TimeSeries = new TimeSeries();
DataSource="@TimeSeries" TValue="TimePoint"
I use this list implementation to automatically set unique Ids when the items are stored into the list.
Best regards, Bruno
Hi Bruno,
We have checked your query and before proceeding further with your requirement kindly share the below details to validate further at our end.
The above-requested details will be very helpful for us to validate the reported query at our end and provide the solution as early as possible.
Regards,
Naveen Palanivel
Hi Naveen
My problem is that the Add method of the list is never called, when the user adds new records into the grid. In the Add method I would like to set the new Id. How get new records stored into the supplied list by default? Is this done by reflection?
I think it's better to start from scratch:
Your Order sample has an OrderID column that must be edited by the user to get different Ids.
But I don't want the user to edit any Ids. So I'm looking for a solution to set the OrderID automatically.
The OrderIDs should start at a predefined value and incremented each time a new record is added. The OrderID column must not be visible.
How can I achieve this?
Regards, Bruno
Hi Bruno
Sorry for the delay
We understand your query, you need to start the OrderIDs at a specific value and increase them every time a new record is added. Also, the OrderID column should not be visible to the user. Based on your requirement we prepared the sample . Please refer the code snippet and attached sample for your reference.
<SfGrid DataSource="@Orders" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })"> <GridEvents OnBatchAdd="BatchAddHandler" TValue="Order"></GridEvents>
<GridColumns> <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" IsIdentity="true" AllowEditing="false" DefaultValue="@Count" Visible=false AllowAdding="false" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</SfGrid>
@code { public List<Order> Orders { get; set; } public int? Count = 32341;
public void BatchAddHandler(BeforeBatchAddArgs<Order> args) { args.DefaultData.OrderID = args.DefaultData.OrderID + 1;
Count = args.DefaultData.OrderID;
} |
Please let us know if you have any concerns.
Regards,
Naveen Palanivel
Hi Naveen
That works perfectly with the ORDERID. Thank you very much!
But I have another problem:
I added:
async Task OnCellSaved(CellSaveArgs<Order> e)
{
await Grid!.EndEditAsync();
}
for automatic updating as you suggested in other posts.
When I add a new record and want to delete it while editing I get an Index out of range exception:
How can I get rid of this exception?
Regards, Bruno
Hi Burno,
Sorry for the delay,
We have checked your query and before proceeding further with your requirement kindly share the below details to validate further at our end.
The above-requested details will be very helpful for us to validate the reported query at our end and provide the solution as early as possible.
Regards,
Naveen Palanivel
Hi Naveen
The full source code of the page as follows:
@page "/orderstest"
@using Syncfusion.Blazor.Grids
<SfGrid @ref="@Grid" DataSource="@Orders" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })">
<GridEvents CellSaved="OnCellSaved" OnBatchAdd="OnBatchAdded" TValue="Order"></GridEvents>
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Batch" ShowConfirmDialog="false" NewRowPosition="NewRowPosition.Bottom"></GridEditSettings>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" IsIdentity="true" AllowEditing="false" DefaultValue="@Count" Visible=true AllowAdding="false" 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 {
SfGrid<Order>? Grid { get; set; }
public List<Order> Orders { get; set; }
public int? Count = 32341;
protected override void OnInitialized()
{
Orders = Enumerable.Range(1, 5).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; }
}
async Task OnCellSaved(CellSaveArgs<Order> e)
{
await Grid!.EndEditAsync();
}
public void OnBatchAdded(BeforeBatchAddArgs<Order> args)
{
args.DefaultData.OrderID = args.DefaultData.OrderID + 1;
Count = args.DefaultData.OrderID;
}
}
Hi Burno,
We checked your query, we made
the some work around solution to overcome the issue. We also attached the
modified sample for your reference
public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args) {
if (args.Item.Text == "Delete") { if (Grid.IsEdit) {
Grid.CloseEditAsync();
await Task.Delay(1000);
} } }
|
Please let us know if you have any concerns.
Regards,
Naveen Palanivel