Unplanned
Last Updated: 13 Feb 2024 10:56 by ADMIN
Avrohom Yisroel
Created on: 28 Jun 2023 11:11
Category: Grid
Type: Bug Report
2
Grid stays in edit mode infinitely when adding a new item if the component is bound to ObservableCollection

I have a Grid bound to ObservableCollection and I try to add a new record, the Grid stays in edit mode even if I click the Save button. 

Workaround:

@using System.Collections.ObjectModel
@using Telerik.FontIcons

<TelerikGrid Data="@Cards"
             Height="800px"
             FilterMode="GridFilterMode.FilterRow"
             EditMode="GridEditMode.Inline"
             OnCreate="NewCard"
             OnUpdate="UpdateCard"
             OnDelete="DeleteCard"
             ConfirmDelete="true"
             @ref="@GridReference">
    <GridToolBarTemplate>
        <GridCommandButton Command="Add" Icon="@FontIcon.Plus">New</GridCommandButton>
    </GridToolBarTemplate>
    <GridColumns>
        <GridColumn Field="@nameof(AdminCard.SerialNumber)" Title="Serial number" />
        <GridColumn Field="@nameof(AdminCard.DisplayNumber)" Title="Display number" />
        <GridColumn Field="@nameof(AdminCard.Name)" />
        <GridCommandColumn Width="140px">
            <GridCommandButton Command="Save" Icon="@FontIcon.Save" ShowInEdit="true" />
            <GridCommandButton Command="Edit" Icon="@FontIcon.Pencil" />
            <GridCommandButton Command="Delete" Icon="@FontIcon.Trash" />
            <GridCommandButton Command="Cancel" Icon="@FontIcon.Cancel" ShowInEdit="true" />
        </GridCommandColumn>
    </GridColumns>
</TelerikGrid>

@code {
    TelerikGrid<AdminCard> GridReference { get; set; }

    public ObservableCollection<AdminCard> Cards { get; set; } = new();

    protected override async Task OnInitializedAsync() =>
      Cards = new ObservableCollection<AdminCard>{
        new AdminCard{SerialNumber="123", DisplayNumber="1234 1234", Name="Jim"}
                      };

    private async Task NewCard(GridCommandEventArgs args)
    {
        AdminCard card = (AdminCard)args.Item;
        Cards.Add(card);

        //apply this after saving the new record to the database
        var gridState = GridReference.GetState();
        gridState.InsertedItem = null;
        gridState.OriginalEditItem = null;
        await GridReference.SetStateAsync(gridState);
    }

    private async Task UpdateCard(GridCommandEventArgs args)
    {
        AdminCard fromGrid = (AdminCard)args.Item;
        AdminCard existing = Cards.Single(c => c.Id == fromGrid.Id);
        existing.SerialNumber = fromGrid.SerialNumber;
        existing.DisplayNumber = fromGrid.DisplayNumber;
        existing.Name = fromGrid.Name;
    }

    private async Task DeleteCard(GridCommandEventArgs args)
    {
        AdminCard card = (AdminCard)args.Item;
        Cards.Remove(card);
    }

    public class AdminCard
    {
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public string SerialNumber { get; set; } = "";
        public string DisplayNumber { get; set; } = "";
        public string Name { get; set; } = "";
    }
}

3 comments
ADMIN
Nadezhda Tacheva
Posted on: 13 Feb 2024 10:56

Hello Adam,

Thank you for your input! I will update the editing article to include a link to the listed knowledge base article.

Regards,
Nadezhda Tacheva
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!
Adam
Posted on: 08 Feb 2024 20:24

I have been bit by this before, and I always forget about it when I do another Blazor project.

There is now documentation here, but it is not ( Not that I can find ) listed on the actual grid edit documentation.

I was just bit by it again, and stumbled on this post and the page listed below accidentally.

https://docs.telerik.com/blazor-ui/knowledge-base/grid-add-edit-state

I agree this needs to be referenced in the actual Edit documentation, as ObservableCollections are my norm for UI development, as I switch between WPF, Blazor, and Hybrid.

 

Avrohom Yisroel
Posted on: 06 Feb 2024 19:32

As this issue is not planned for fixing, please can I request that you make a note on the docs and demos pages about it. Otherwise people will waste time trying to work out why the grid doesn't exit edit mode.

Given that it is recommended practice to bind Blazor controls to ObservableCollections, I would say this is an important issue, and the least you should do is make people aware of the bug.