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

Commands, QDSCV, MVVM

4 Answers 79 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Heiko
Top achievements
Rank 1
Iron
Veteran
Heiko asked on 23 Oct 2012, 05:22 AM
I extended the GridView Commands example by using a ViewModel behind and binding the GridView to a QDSCV in the VM. So far this is working as expected. A new row is added to the QDSCV by calling RadGridViewCommand.BeginInsert- fine. The example stops at the point where I want to persist the data to a database.
The problem is when calling RadGridViewCommands.CommitEdit or RadGridViewCommands.CancelRowEdit. Since these two commands just do a "CommitEdit/New" or "CancelEdit/New" but no "SubmitChanges" or "RejectChanges" I have to find a place where to "hook in" and add this code. On the VM side I don't know how to distinguish between commiting and cancelling the edit mode. I tried to check the PropertyChanged event of QDSCV but both CommitEdit and CancelRowEdit set CurrentAddItem to NULL and IsAddingNew to false. Any hints where to start?

Regards
Neils

4 Answers, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 25 Oct 2012, 07:51 AM
Hi,

Unfortunately there is no way to distinguish which is the edit action of the row edit ended event from a view model. You can use DomainContext.HasChanges property, just ensure that you are using one and the same instance of a DomainContext class.
Be aware that these methods (SubmitChanges() and RejectChanges()) will perform a call to the underlying database, therefore you should consider if you need it for every row edit or perform a bulk operation (to update all items at once).

Kind regards,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Heiko
Top achievements
Rank 1
Iron
Veteran
answered on 25 Oct 2012, 08:56 AM
Hi,

and thanks for the answer. Meanwhile I found a solution for this by using events on the grid and from there calling methods in my ViewModel. Here is my code; could you please confirm that this is the right way to do it?

private void GrdNotesRowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
{
    switch (e.EditAction)
    {
        case GridViewEditAction.Commit:
            VM.NotesSaveChanges();
            break;
        case GridViewEditAction.Cancel:
            VM.NotesRejectChanges();
            break;
    }
}
 
private void GrdNotesDeleting(object sender, GridViewDeletingEventArgs e)
{
    // important: e.Cancel = true!
    e.Cancel = true;
    VM.NotesDeleteItems(e.Items);
}

0
Nedyalko Nikolov
Telerik team
answered on 25 Oct 2012, 02:03 PM
Hi,

Indeed using RadGridView events is the right way to do this. This breaks "pure MVVM" pattern a little bit (that's why I do not suggest it to you). According to me using MVVM pattern should make your life easier and there is no need to reject all old fashioned events, because events are not compliant with the "pure" MVVM pattern.

Greetings,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Heiko
Top achievements
Rank 1
Iron
Veteran
answered on 25 Oct 2012, 02:43 PM
Thanks Nedyalko for your answer. I totally agree with you that it is not "pure" MVVM, but one has to be pragmatic. The event is triggering a method on the VM and this is a scenario which is testable so that's fine with me.

Best regards
Heiko
Tags
GridView
Asked by
Heiko
Top achievements
Rank 1
Iron
Veteran
Answers by
Nedyalko Nikolov
Telerik team
Heiko
Top achievements
Rank 1
Iron
Veteran
Share this question
or