I am using Entity Framework to communicate with an SQLServer database and am using WinGrid to view the database records. To edit, add or delete a record, I call up a separate detail form. If I am editing or deleting a record, when the changes are made, the WinGrid reflects the changes immediately and the altered record appears in the grid (or disappears in the case of a delete). When adding an new record, however, the record is not visible in the grid after performing a "SaveChanges()" command to save the new record to the database. If the program is closed and then restarted, the record appears in the grid as it should. I have tried every refresh command I can think of on all of the components, including the grid, the bindingsource and the EF components and nothing works to show the new record in the grid.
Any suggestions?
Hi Larry,
thanks for your message.Indeed, the ToList() extension method will work here.
The side effect is that it casts the queryable/objectcontext to a real List<xxx>, pulling all the data from the database. While this might be ok for most purposes, there might be situations where you want to stick to a queryable context (ObjectQuery).
Beside that I belive this solution will be fine for the majority of cases.
cheers and best regardsAndy
hi I have a better solution to your problem
when initially binding up with linq2sql;
BindingSource.DataSource = DataContext.YourTable.ToList()
you only have to do this once, if you connect your grid.DataSource to the BindingSource you can do DataContext.YourTable.Add(new Entity) and they show up.
Took me a while to figure out what was going on but saves cluttering code behind with work around code
You are wellcome Steve.This bothered me for quite a while so I am happy that it works for you too.Let me know if you found out about any side effects, you never know ;)
cheersAndy
Andy;
Your solution works in my program as well. I'm still not sure why all this is needed, but it does appear to work. I will continue to test it and make sure there are no "side effects".
Many thanks for working this problem out!
Steve
Hi Mike and Steve
I figured it out. It has nothing to do with the UltraGrid, as Steve already confirmed that it also doesn't work with GridView.
I searched for a while and came up with the following solution:
grdAccounts.DataSource = _dataContext.tblAccount.Execute(MergeOption.AppendOnly); grdAccounts.Refresh();
You need to call this if you bind the first time and after you added new rows.For deletes and updates refreshing the grid is sufficient.
Hope it helps.