How to detect changes of bindingsource bound to entity?
I have a bindingsource which is bound to entity.
normBindingSource.DataSource =
dowacodbEntities.norms.OrderBy(o1 => o1.UsePurpose_id).ThenB开发者_Go百科y(o2 => o2.Quantity);
Then a bindingsource is displayed in a datagridview. If I use dataset, I can call HasChanges() method to check if there are changes have been made by user. Moreover, I found context.ObjectStateManager.GetObjectStateEntries(...) seems to be what I am looking for but I don't know how to use it.
My purpose is when user leaves form, there will be a message box shows : "There are some changes, would you like to save it before exit ?"
Try this. I also encountered that.
create a class that checks for modifications.
//define first your datacontext
List<dowaCodbEntities> dowacodbEntities;
private bool CheckForModifications()
{
List<dowaCodbEntities> dowaCodbEntitiesCopy = normBindingSource.DataSource.Cast<dowaCodbEntities>().ToList();
if(dowacodbEntities.Count != dowaCodbEntitiesCopy.Count)
return true;
}
just call that function anytime you need it, mostly on closing. Then if it returns true, stop closing first and ask if changes want to be made. Let me know if you need anything else.
This is the best method so far :
bool changesMade = context.
ObjectStateManager.
GetObjectStateEntries(EntityState.Added |
EntityState.Deleted |
EntityState.Modified
).Any();
精彩评论