Calling SetCellValue after AddRecord don't update value.

Hi,

I have to change the row data after inserting it.
If i add record and after i edit it with SetCellValue doesn't work, but if i edit the DataSource works.
I set the edit grid mode in Mode="EditMode.Normal" otherwise AddRecord add doesn't work.
 
Where i wrong?

I simplified the real case, but in order to convert to a existing project i need to work directly on the grid or dataset and not on the Order object before insert.

Thanks!


@page "/"
@using Syncfusion.Blazor.Grids


<button class="btn btn-primary" @onclick="SetCellValue">BySetCellValue_NOWORK</button>
<button class="btn btn-primary" @onclick="DataSourceSetValueReflectionNotWorks">ByDataSource_WORKS</button>



<SfGrid @ref="@myGrid" DataSource="@Orders">
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings>
    <GridColumns>
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120" IsPrimaryKey="true"></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; }
    public SfGrid<Order> myGrid { get; set; }

    private void SetCellValue()
    {
        var toAdd = NewOrder();
        myGrid.SetCellValue(toAdd.OrderID, "CustomerID", "SetCellValue!");
    }

    private void DataSourceSetValueReflectionNotWorks()
    {
        var toAdd = NewOrder();
        var obj = myGrid.DataSource.Where(x => x.OrderID == toAdd.OrderID).First();
        obj.GetType().GetProperty("CustomerID").SetValue(obj, "DataSourceSetValueReflectionNotWorks", null);
        StateHasChanged();
    }

    private Order NewOrder()
    {
        var newOrder = myGrid.DataSource.Select(x => x.OrderID).Max() + 1;
        var toAdd = new Order()
        {
            OrderID = newOrder,
            CustomerID = newOrder.ToString(),
            Freight = newOrder,
            OrderDate = DateTime.Now.AddDays(1),
        };
        myGrid.AddRecord(toAdd, 0);
        return toAdd;
    }
     
    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; }
    }
}

Attachment: ServersampleAddRow_c75f8a7f.zip

3 Replies 1 reply marked as answer

JP Jeevakanth Palaniappan Syncfusion Team October 23, 2020 10:09 AM UTC

Hi Marco, 

Greetings from Syncfusion support. 

We have validated your query and we suggest you to use the SetCellValue and AddRecord methods of the grid asynchronously which is the recommended way to achieve your requirement. Please find the below code snippet and the sample for your reference. 

    private async void SetCellValue() 
    { 
       var newOrder = myGrid.DataSource.Select(x => x.OrderID).Max() + 1; 
        var toAdd = new Order() 
        { 
            OrderID = newOrder, 
            CustomerID = newOrder.ToString(), 
            Freight = newOrder, 
            OrderDate = DateTime.Now.AddDays(1), 
        }; 
        await myGrid.AddRecord(toAdd, 0); 
        await myGrid.SetCellValue(toAdd.OrderID, "CustomerID", "SetCellValue!"); 
    } 


Please get back to us if you need further assistance. 

Regards, 
Jeevakanth SP. 


Marked as answer

MA Marco October 26, 2020 11:05 AM UTC

thanks, now it works fine!


JP Jeevakanth Palaniappan Syncfusion Team October 27, 2020 05:47 AM UTC

Hi Marco, 

Thanks for the update. Please get back to us if you need further assistance. 

Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon