在SaveChange不起作用后刷新DataSource

时间:2011-06-29 02:42:29

标签: c# linq c#-4.0 savechanges

我在ObjectSet中添加了数据,并在SaveChanges上执行了ObjectContext。但新数据未显示在DataGrid

代码:

bsWork.DataSource = Program.EC.Addresses;
...
Address newAdr = new Address();
//change properties newAdr
...
Program.EC.Addresses.AddObject(newAdr);
Program.EC.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
如何刷新数据或查看新数据?

UPD:解决方案

...
bsWork.Add(newAdr);
Program.EC.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
...

1 个答案:

答案 0 :(得分:1)

我希望这段代码对您有所帮助。

try {
    // Try to save changes, which may cause a conflict.
    int num = context.SaveChanges();
    Console.WriteLine("No conflicts. " + num.ToString() + " updates saved.");
}
catch (OptimisticConcurrencyException) {
    // Resolve the concurrency conflict by refreshing the 
    // object context before re-saving changes. 
    context.Refresh(RefreshMode.ClientWins, orders);

// Save changes.
context.SaveChanges();
Console.WriteLine("OptimisticConcurrencyException "
    + "handled and changes saved");

}