I've got an entity Complaint. It has a collection of Resolutions. Each resolution has a collection of Actions. So my grid binds to Complaint.Resolutions. And I've got a HierarchyChildTemplate defined which binds to the Actions collection.
My ActionGrid has CanUserDeleteRows = True and CanUserInsertRows = True. I've also set ShowInsertRow = True.
deleting rows keeps triggering an error from EF about the required relationship being null. How do I capture this? I think it has something to do with how the row gets added. Right now I'm binding to the Deleted event and then looping through each item and telling the context to delete it. But the problem is that by then it's already been removed from the collection, so when I try to save, I still get the foreign key is null error.
Trying to capture the deleting event isn't working. When is this event raised? I've tried binding to it, but my command isn't being hit when I press the Delete key for a row. The row is just removed from the collection.
9 Answers, 1 is accepted
I've managed to restructure my viewModel so that the binding is working. I am now hitting the breakpoints. I've since also added a column with a delete button. And I'm noticing something that wasn't visible before. If I click add new row, I am hitting the breakpoint for the AddingNewDataItem event. I'm not hitting a breakpoint for the RowEditEnded event. I've set up the binding in exactly the same way.
Here is my scenerio:
User clicks the new row to add a row. This works. Then changes his mind and uses the escape key to cancel the new row, the new row doesn't disappear. It is still there but not visible. First they hit escape and cancel the edit of the cell, then hit escape again to cancel the new row, the new row isnt' visible (the "new row" row is visible again). But the grid is still in edit mode. (the delete button is greyed out and although you can select another row, you cannot delete it either using the delete key).
Once escape is pressed to cancel the insert, how do I cancel the insert and put the grid back in non-edit mode?
I have prepared an example project for you, implementing a RadGridView scenario with a custom ButtonColumn that invokes the "delete" command. However, when adding a new row, on the second "Esc" key pressing the RowEditEnded event is raised and the RadGridView leaves edit mode (as expected). Would you please send us a simple demo project that replicates this behavior, in order to help us provide you better support to your case . Meanwhile you may refer to the attached project to see if there are any differences between your approach and mine. Please, excuse me for the inconvenience.
the Telerik team
Here is the code myy event handler for RowEditEnded:
I click the new row to create a new row, change my mind and hit ESC twice...once to cancel the cell, once to cancel the row.
When I put a breakpoint at this and step through, the EditAction is correct (Cancel), but it thinks my EditOperationType is Edit not Insert. Why would it think it's an edit, when I clicked the New Row? At what point does it show as an Insert vs an Edit?
I need to reverse the new object created in AddingNewDataItem if the user cancels the insert new row.
Would you please confirm what version of RadControls you are using? I have tested your statements with the latest and several recent versions and I have never faced such a behavior. EditOperationType is always "insert", when cancelling the insertion of a new row. Please, try to reproduce it on the attached project and inform us if this issue persists.
the Telerik team
I've run a few tests and it appears that if I capture the AddingNewDataItem event and set e.NewObject = new Action(); then when I step through RowEditEnded the EditOperationType is Edit. If I don't do anything in the AddingNewDataItem event, the EditOperationType is Insert. After reading the docs a bit more carefully, I realized I don't need the first event since my grid is bound to a entity collection.
I am still back to the original problem now. I'm trying to capture the events in my viewModel. But my binding isn't working.
My User control has a RadTabControl that has an ItemsSource. It also has a defined DataTemplate. Within the dataTemplate is the RadGridView bound to a child collection. I've also defined some interaction triggers as follows:
I'm not sure how to bind these Commands to my ViewModel, since this is inside a DataTemplate. When I had the grid outside the dataTemplate, it worked. How do I modify my Command binding so that it works?
I need to capture these events so that I can tell the ObjectContext to delete the object rather than just remove it from the collection. Otherwise I get an error on saving about Foreign Key cannot be null.
May I kindly ask you again to send me a simple example project, which illustrates these issues. It is very difficult for me to provide you an appropriate solution, without any knowledge of the internal structure of your project. Thank you in advance.
the Telerik team
Thanks again. I can now capture the changed entities and mark them deleted in the context.
I am very glad to hear that you have managed to repair your project. Does this mean that all the listed issues are already solved?
All the best,
the Telerik team