This is a migrated thread and some comments may be shown as answers.

Edit.Commit when cell loses focus

5 Answers 995 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Veteran
Robert asked on 30 Jul 2020, 10:44 AM

I have RadGridView when editable columns. When the user updates the value of a cell and then clicks anywhere else outside the grid, I expected there will be a Edit.Commit, but instead the cell is still in edit mode until you go in to that cell and hits Enter (or clicks on another row in the grid) . 

 

How do I make this happen, MVVM-approach if possible

 

 

Many thanks in advance, Robert

5 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 04 Aug 2020, 05:32 AM

Hello Robert,

You can try using GridView's CommitEdit() method to commit the edit operation once RadGridView loses focus. For example, you can subscribe to GridView's LostFocus event and invoke the method once it fires. Please give this approach a try and let me know how it works for you.

Regards,
Yoan
Progress Telerik

0
Rex
Top achievements
Rank 1
answered on 28 Jan 2021, 12:37 AM
You can do it like this. Subscribe to the event as Yoan suggested:
 
<telerik:EventToCommandBehavior.EventBindings>
<telerik:EventBinding Command="{Binding DataContext.LostFocusAction ,RelativeSource={RelativeSource AncestorType={x:Type local:MyView}, Mode=FindAncestor}}" EventName="LostFocus" PassEventArgsToCommand="True" />
</telerik:EventToCommandBehavior.EventBindings>

 

Then in the view model do this:

LostFocusAction = new DelegateCommand<RoutedEventArgs>(o =>
{
    if (o.Source is RadGridView grid && grid.RowInEditMode != null)
    {
        grid.CommitRowEdit(grid.RowInEditMode);
    }
});

 

Yoan, this should be the default behavior for this control. Our expectation is that when the cell loses focus, it should commit. Please add for next release.

0
Rex
Top achievements
Rank 1
answered on 28 Jan 2021, 01:29 AM
Actually, my solution only works once! Then it gets trapped in a loop of lost focus events effectively locking the cell. I can't get the LostFocus event to fire from the GridViewDataColumn that is a textbox. Maybe that's why the behavior is occurring in the first place.
0
Rex
Top achievements
Rank 1
answered on 28 Jan 2021, 01:38 AM

Ok. Its hacky. But this works for me. You have to subscribe to the LostFocus event of the RadGridView itself, then to prevent the loop from occurring you have to check the original source (make sure its not the RadGridView) like this

LostFocusAction = new DelegateCommand<RoutedEventArgs>(o =>
{
    if (!(o.OriginalSource is RadGridView) && o.Source is RadGridView grid && grid.RowInEditMode!=null)
    {
        var row = grid.RowInEditMode;
        grid.RowInEditMode.CommitEdit();
        o.Handled = true;
    }
    else
    {
        o.Handled = true;
    }
});
0
Yoan
Telerik team
answered on 01 Feb 2021, 03:51 PM

Hello Rex,

Generally, when RadGridView loses focus it will invoke the CommitEdit operation if there is a row in edit mode. This is the default behavior and it is controlled by the ActionOnLostFocus property of the RadGridView (its default value is CommitEdit). Can you please check if this property is properly set to your grid? If this is not the case, please let me know and I will be happy to assist you further.

Please keep in mind that this will happen only if the GridView loses the focus, not the cell. This is by design because the commit operation is executed for the data item which is represented by the whole GridVIewRow. This design allows you to edit a cell, move to the next cell and edit it, and so on.

Regards,
Yoan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
commented on 25 Apr 2022, 02:34 PM | edited

Hi Yoan, sorry for necroing this but what you said in your last paragraph was exactly what I needed to know.
To prevent this behavior, I attached a behavior to my RadGridView, which code is simply:

public class CommitEditOnSelectionChangeBehavior : Behavior<RadGridView>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.SelectedCellsChanged += OnRadGridViewSelectedCellsChanged;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.SelectedCellsChanged -= OnRadGridViewSelectedCellsChanged;
        base.OnDetaching();
    }

    private void OnRadGridViewSelectedCellsChanged(object sender, GridViewSelectedCellsChangedEventArgs args)
    {
        // As we change the selection, end any edition
        AssociatedObject.CommitEdit();
    }
}

It seems to work: when I edit a cell, then click on another one, it ends edition.

But is it possible that you add some kind of "policy" (maybe an enum?) so I don't have to code it myself with the behavior above? That would be a more elegant solution imo!

Thanks for your reply,

Arthur

Martin Ivanov
Telerik team
commented on 28 Apr 2022, 11:24 AM

Can you tell me a bit more about your requirement? Do you need to commit the row edit each time you change the current cell? Can you tell me why do you need this behavior?
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
commented on 28 Apr 2022, 12:06 PM | edited

HI Martin,

What I need is to go out of "edit mode"' when I click on another cell or outside the grid: the fact that I need to "commit the row" to accomplish this is just business stuff to me.

What I mean is, I don't specifically need to commit the row, it's just a way for me to achieve the behavior I want; if there is a better way, please let me know!

I need this behavior because visually, it's how Excel works: when you select another cell, you go out of edit mode, even if you were in edit mode in the previously selected cell. And our users want to use our tool just like Excel because it feels more natural to them!

 

Martin Ivanov
Telerik team
commented on 03 May 2022, 11:17 AM

Thank you for the additional information. I think I got a better idea about your requirement now. There is no built-in feature that allows this, but if you have some specific ideas you can share them in the feedback portal.

Currently, the most convenient approach for your case is your solution - where the edit is committed when the selected cell is changed. An alternative entry point that you can use is the CurrentCellChanged event if SelectedCellsChanged doesn't work in your situation for some reason.

Tags
GridView
Asked by
Robert
Top achievements
Rank 1
Veteran
Answers by
Yoan
Telerik team
Rex
Top achievements
Rank 1
Share this question
or