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
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
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.
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
;
}
});
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/.
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
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!
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.