I am using a datagrid, which I sometimes admit to be very hard to use especially when its editable.
There is no clear property on how to get even just the currently displayed data.
I have a code that updates the data using UpdateCellAsync, and I have to get that data on a different method.
Using GetCurrentViewRecordsAsync, which should return the current visible data of grid, does not work.
Even though the grid already shows the new value, GetCurrentViewRecordsAsync still returns the old data.
My existing methods:
OnItemSelected - change item on cell using UpdateCellAsync, the grid already displays the new item selected
RefreshFilter - get all rows on grid using GetCurrentViewRecordsAsync, but item still returns old cell data
I already tried the following that might be able to help but to no avail:
I think GetBatchChanges could return the data i need, but it includes more work when it should just be easy.
Hi Noel,
Greetings from Syncfusion.
We suspect that you want to get the updated data(updated using the UpdateCellAsync method) without saving updated values to the Grid(Click the Update button to save the values/call the EndEditAsync method).
Normally while using the UpdateCellAsync method to update the cell value, the value is updated at the UI level only and shown the green color for the updated cell. If you want get the updated values while using the GetCurrentViewRecordsAsync method, then you need to save updated changes(using the EndEditAsync method/ using the Update button in the toolbar) to the Grid.
Here, we have updated the cell value using the UpdateCellAsync method of the Grid. Then we have called the EndEditAsync method of the Grid to save the changes. Now the updated value is shown while getting the CurrentViewRecordsAsync method.
|
<button @onclick="Update">Update Cell Value</button>
<button @onclick="GetCurrentView">Get current view records</button>
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })" Height="315"> <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch" ShowConfirmDialog="false"></GridEditSettings> <GridColumns> <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" ValidationRules="@(new ValidationRules { Required = true })" Type="ColumnType.Number" Width="120"></GridColumn> <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" ValidationRules="@(new ValidationRules{ Required=true})" Width="120"></GridColumn> .. . </GridColumns> </SfGrid>
@code{ SfGrid<Order> Grid; public List<Order> Orders { 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), ShipCountry = (new string[] { "USA", "UK", "CHINA", "RUSSIA", "INDIA" })[new Random().Next(5)] }).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 string ShipCountry { get; set; } } public async Task Update() { await Grid.UpdateCellAsync(1, "CustomerID", "New Updated value"); await Grid.EndEditAsync(); //calling EndEditAsync method to save the changes } public async Task GetCurrentView() { var data = await Grid.GetCurrentViewRecordsAsync(); } } |
|
|
Please let us know if you have any concerns.
Regards,
Rahul
This seems to be working, though I am not sure why it did not work when I tried it first.
Anyway, I just want to have a followup question:
Is there a way to programmatically save the grid? Just like having the built-in save button on toolbar.
Hi Noel,
Thanks for the update.
Query: Is there a way to programmatically save the grid?
You can save the edited records programmatically by using the EndEditAsync method of the Grid. Find the below reference and code snippets for your reference.
|
<button @onclick="Update">Update Cell Value</button>
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true" Toolbar="@(new List<string>() { "Add", "Delete", "Update", "Cancel" })" Height="315"> <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch" ShowConfirmDialog="false"></GridEditSettings> <GridColumns> <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" ValidationRules="@(new ValidationRules { Required = true })" Type="ColumnType.Number" Width="120"></GridColumn> <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" ValidationRules="@(new ValidationRules{ Required=true})" Width="120"></GridColumn> .. . </GridColumns> </SfGrid>
@code{ SfGrid<Order> Grid; public List<Order> Orders { get; set; }
. . . public async Task Update() { . . . await Grid.EndEditAsync(); //calling EndEditAsync method to save the changes }
} |
Please let us know if you have any concerns.
Regards,
Rahul