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

DataBinding to EF entity collection

9 Answers 203 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rayne
Top achievements
Rank 1
Rayne asked on 04 May 2011, 07:20 PM
I'm using entity framework and binding a GridView to a entity collection.

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

Sort by
0
Rayne
Top achievements
Rank 1
answered on 04 May 2011, 10:31 PM
I've made some progress. But I'm still having a bit of trouble.

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?
0
Ivan Ivanov
Telerik team
answered on 09 May 2011, 11:10 AM
Hello Rayne,

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.

Kind regards,
Ivan Ivanov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rayne
Top achievements
Rank 1
answered on 09 May 2011, 09:11 PM
I'm finally hitting the event triggers. I've got one for AddingNewDataItem that creates a new entity object and assigns it to NewObject.
Here is the code myy event handler for RowEditEnded:
if (e.EditAction == GridViewEditAction.Cancel)
{
   if (e.EditOperationType == GridViewEditOperationType.Insert)
   {   //canceling an insert - remove inserted action 
      var action = e.Row.DataContext as DAction;
      _dataService.DeleteDAction(action);
   }
}
e.Handled = true;

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.
0
Ivan Ivanov
Telerik team
answered on 11 May 2011, 12:54 PM
Hello Rayne,

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.

Kind regards,
Ivan Ivanov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rayne
Top achievements
Rank 1
answered on 11 May 2011, 03:20 PM

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:Interaction.Triggers>
   <i:EventTrigger EventName="Deleting">
      <cmd:EventToCommand PassEventArgsToCommand="True"  Command="{Binding DeletingActionCommand}" />
   </i:EventTrigger>
   <i:EventTrigger EventName="RowEditEnded">
      <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding ActionEditEnded}" />
   </i:EventTrigger>
</i:Interaction.Triggers>

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.

0
Ivan Ivanov
Telerik team
answered on 16 May 2011, 04:36 PM
Hello Rayne,

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.

Greetings,
Ivan Ivanov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rayne
Top achievements
Rank 1
answered on 16 May 2011, 05:43 PM
While trying to put together a sample to send, I figured out how to get it to work. I named the UserControl itself in the xaml using the Name property, then I was able to bind to the DataContext.CommandName of the UserControl via the ElementName binding syntax.

Thanks again. I can now capture the changed entities and mark them deleted in the context.
0
Ivan Ivanov
Telerik team
answered on 17 May 2011, 09:04 AM
Hi Rayne,

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,
Ivan Ivanov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rayne
Top achievements
Rank 1
answered on 17 May 2011, 02:38 PM
Yes, thank you. all is resolved for this thread.
Tags
GridView
Asked by
Rayne
Top achievements
Rank 1
Answers by
Rayne
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Share this question
or