简体   繁体   English

数据网格,实体框架WPF应用程序中的“更新”按钮

[英]Update button in datagrid, entity framework WPF Application

I'm attempting to create an update button, that you can press to automatically update a record within a DB. 我试图创建一个更新按钮,您可以按该按钮来自动更新数据库中的记录。

the update button opens a new dialog, which then allows user to enter the updated details in text boxes and hit submit. 更新按钮将打开一个新对话框,然后该对话框允许用户在文本框中输入更新的详细信息并单击“提交”。 This should then change the record within the DB and refresh the datagrid. 然后,这应该更改数据库中的记录并刷新数据网格。

The code wont update the record within the DB. 该代码不会更新数据库中的记录。

[code removed for personal reasons] [出于个人原因删除了代码]

While it would be better if you could refactor this in a proper MVVM style, your main problem is that your Student s2 is not attached to the context so there are no changes to be saved. 如果可以使用适当的MVVM样式来重构它会更好,但是主要问题是Student s2未附加到上下文,因此没有要保存的更改。 Attach it to the context : 将其附加到上下文:

Student s2 = (Student)button.DataContext;
context.Students.Attach(s2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM