How to cancel grid edit mode programmatically

1 Answer 963 Views
Grid
Robert
Top achievements
Rank 1
Robert asked on 07 Dec 2021, 07:49 AM

Hi,

how can I cancel the edit mode programmatically in Inline edit mode?

1 Answer, 1 is accepted

Sort by
1
Accepted
Apostolos
Telerik team
answered on 07 Dec 2021, 03:27 PM

Hi Robert,

It is possible to exit edit mode programmatically, without using the default Cancel button.

To achieve this, you can use the Grid State. We have a section that shows how to initiate editing. In your case, you will do the opposite - set the following Grid state properties to null:

  • InsertedItem
  • EditItem
  • OriginalEditItem

 The code snippet bellow uses an OnClick event handler as an example. Note that the edited item will not be saved.

<TelerikButton OnClick="@CancelEdit">Exit Edit Mode</TelerikButton>

<TelerikGrid @ref="@GridRef" />

@code {
    TelerikGrid<GridModel> GridRef { get; set; }

    async Task CancelEdit()
    {
        var state = GridRef.GetState();
        state.EditItem = null;
        state.InsertedItem = null;
        state.OriginalEditItem = null;
        await GridRef.SetState(state);
    }
}

I hope you find the above information useful.

Regards,
Apostolos
Progress Telerik

Learn about the important changes coming in UI for Blazor 3.0 in January 2022!
Robert
Top achievements
Rank 1
commented on 07 Dec 2021, 03:36 PM

Hi Apostolos!

Thanks for the answer.

A suggestion: why not to provide a simple public method on the Grid?

Apostolos
Telerik team
commented on 08 Dec 2021, 04:11 PM

Hello Robert,

Normally, the Grid Cancel command allows users to cancel changes in inline edit mode. It appears that it is not suitable for your case.

I would like to get more details about your scenario and what brings the need to exit edit mode programmatically. This will allow us to understand your needs better.

Thank you in advance!

Regards,

Apostolos
Progress Telerik

Learn about the important changes coming in UI for Blazor 3.0 in January 2022!
Leland
Top achievements
Rank 2
Iron
Iron
Iron
commented on 13 Sep 2024, 05:09 PM

I can confirm that using the Grid State in this way still works.

However, I want to point out that `OriginalEditItem`, `EditItem`, and `InsertedItem` are non-nullable types, so you get a warning that you cannot convert null literal to non-nullable reference type.  Making them nullable would remove the warning and better communicate the intent that setting a null value can cancel the edit.

Dimo
Telerik team
commented on 16 Sep 2024, 12:05 PM

@Leland - I agree with you and I stumble on this issue myself. We have an internal task about nullable-related refactoring in our source code. The bad thing is that this task requires development effort, which does not match the resulting small business value. As a result, the priority is low.

Please consider the null forgiving operator, if you haven't already.

Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Answers by
Apostolos
Telerik team
Share this question
or