Edit.Commit when cell loses focus

6 posts, 0 answers
  1. Robert
    Robert avatar
    23 posts
    Member since:
    Apr 2020

    Posted 30 Jul 2020 Link to this post

    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

  2. Yoan
    Admin
    Yoan avatar
    1215 posts

    Posted 04 Aug 2020 Link to this post

    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

  3. Rex
    Rex avatar
    3 posts
    Member since:
    Aug 2018

    Posted 27 Jan Link to this post

    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.

  4. Rex
    Rex avatar
    3 posts
    Member since:
    Aug 2018

    Posted 27 Jan in reply to Rex Link to this post

    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.
  5. Rex
    Rex avatar
    3 posts
    Member since:
    Aug 2018

    Posted 27 Jan in reply to Rex Link to this post

    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;
        }
    });
  6. Yoan
    Admin
    Yoan avatar
    1215 posts

    Posted 01 Feb Link to this post

    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/.

Back to Top