Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity not providing up to date info in a WPF datagrid C#
    text
    copied!<p>I have a datagrid on a WPF window and it shows a list of my users. It works fine with this code.</p> <pre><code>private void UpdateGrid() { var query = db.Users; GridUsers.ItemsSource = query.ToArray(); } </code></pre> <p>There is an edit button that opens a new window on top of this previous window and lets you edit one of the users. When I have finished editing there is a save which calls db.savechanges. This all works fine and if I put a breakpoint there the change has happened in my SQL database. This window is then closed and the UpdateGrid method is called again on the users screen. The datagrid does not update to show the new values in the SQL table. I also have another button that adds and removes users and when the UpdateGrid method is called after performing one of these tasks the datagrid is up to date.</p> <p>Why would this not be updating my datagrid correctly?</p> <p>UPDATE: I have fixed this problem. I am still not sure why it was happening though. My users screen had a global User variable that starts off null. The following code sets it when the edit button is pressed.</p> <pre><code>WorkingUser = (User) GridUsers.SelectedItem; </code></pre> <p>The edit screen then used the following code to get the correct entity.</p> <pre><code>User editUser = (from s in db.Users where (s.UserName == ManageUsers.WorkingUser.UserName) select s).FirstOrDefault(); </code></pre> <p>When db.users.remove(editUser) is called and then db.savechanges is called, the datagrid update works fine. However when I edit editUser and call db.savechanges, the dataGrid does not show correctly. I realised that I did not have to do the select and just used the follow code instead.</p> <pre><code>User editUser = ManageUsers.WorkingUser </code></pre> <p>Now when I edit the editUser updateGrid works fine.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload