Completed
Last Updated: 14 Nov 2024 09:25 by ADMIN
Release 7.0.0
Created on: 30 Aug 2024 10:38
Category: Grid
Type: Bug Report
2
Cancelling OnUpdate during in-cell editing doesn't prevent cell closing

I am using InCell Grid editing. I want to prevent an edit cell from closing on certain condition. However, when I cancel the update with args.IsCancelled = true in OnUpdate, the user can still close the edited cell with Enter or Tab. This is inconsistent with inline or popup editing.

===

A possible workaround is to cancel both OnUpdate and the subsequent OnEdit.

https://blazorrepl.telerik.com/QykMHkks10APuDLQ47

@using System.ComponentModel.DataAnnotations

<TelerikGrid Data="@GridData"
             EditMode="@GridEditMode.Incell"
             OnEdit="@OnGridEdit"
             OnUpdate="@OnGridUpdate">
    <GridColumns>
        <GridColumn Field="@nameof(SampleModel.Name)" />
        <GridColumn Field="@nameof(SampleModel.Min)" />
        <GridColumn Field="@nameof(SampleModel.Max)" />
    </GridColumns>
</TelerikGrid>

<TelerikNotification @ref="@NotificationRef"
                     HorizontalPosition="@NotificationHorizontalPosition.Center"
                     VerticalPosition="@NotificationVerticalPosition.Top" />

@code {
    private TelerikNotification? NotificationRef { get; set; }

    private List<SampleModel> GridData { get; set; } = new();

    private bool ShouldCancelOnEdit { get; set; }

    private int LastId { get; set; }

    private void OnGridEdit(GridCommandEventArgs args)
    {
        if (ShouldCancelOnEdit)
        {
            ShouldCancelOnEdit = false;
            args.IsCancelled = true;
        }
    }

    private void OnGridUpdate(GridCommandEventArgs args)
    {
        var updatedItem = (SampleModel)args.Item;

        if (updatedItem.Min > updatedItem.Max)
        {
            NotificationRef?.Show(new NotificationModel()
            {
                ThemeColor = ThemeConstants.Notification.ThemeColor.Error,
                Text = "Min must be smaller than Max"
            });

            args.IsCancelled = true;
            ShouldCancelOnEdit = true;
        }
        else
        {
            var originalItemIndex = GridData.FindIndex(i => i.Id == updatedItem.Id);

            if (originalItemIndex != -1)
            {
                GridData[originalItemIndex] = updatedItem;
            }

            ShouldCancelOnEdit = false;
        }
    }

    protected override void OnInitialized()
    {
        for (int i = 1; i <= 5; i++)
        {
            GridData.Add(new SampleModel()
            {
                Id = ++LastId,
                Name = $"SampleModel {LastId}",
                Min = Random.Shared.Next(1, 10),
                Max = Random.Shared.Next(11, 20)
            });
        }
    }

    public class SampleModel
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; } = string.Empty;
        public int Min { get; set; }
        public int Max { get; set; }
    }
}

0 comments