Should deleting rows in a bound DataGridView set EntityState to deleted?
-
Tuesday, August 07, 2012 8:10 PM
Hello,
I'm using EF4 in Windows Forms. My code is VB.Net. I use a BindingSource for all DataGridViews. I do not use the BindingNavigator.
I'm trying to determine something:
In a Winforms DataGridView bound to a BindingSource whose DataSource is an ObjectResult(Of T), if a row is deleted - should the underlying entity's state be set to EntityState.Deleted?
If so, couldn't I then call .SaveChanges on the context in the BindingSource.ListChanged event to delete that entity? (ListChangedType would of course be ListChangedType.ItemDeleted)
All Replies
-
Wednesday, August 08, 2012 7:14 PMModerator
Hi,
ListChanged actually finishes before the delete has flowed through to Entity Framework. So you can't call SaveChanges in that event and have the data binding work.
However, the UserDeletedRow event on the data grid should be late enough for you to call SaveChanges and have it work. Just be sure to check that it isn't fired in any situations that aren't right for your the behaviour you want.
We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.
- Proposed As Answer by Glenn CondronMicrosoft Employee, Moderator Wednesday, August 08, 2012 7:14 PM
- Marked As Answer by KorLibrary Wednesday, August 08, 2012 9:18 PM
-
Wednesday, August 08, 2012 9:19 PM
Excellent information, many thanks.

