使用EF更新SelectedItem-WPF C# [英] Update SelectedItem Using EF - WPF C#
问题描述
我正在创建一个应用程序,以使我开始使用WPF并使用实体框架允许CRUD操作.
到目前为止,我已经能够从数据网格添加和删除项目,该数据网格从数据库中加载内容.
但是我的更新方法有问题.
在数据网格中,当我选择一行并对其进行更新时,它将更新表的第一行,而不是我选择的行(例如,第三行向下).
我已经发布了我认为很重要的代码片段.
I am creating an application to get me started with WPF and using entity frameworks to allow CRUD operations.
So far I am able to add and delete items from a data-grid which loads the contents from a database.
But I''m having problems with my update method.
In the data-grid, when i select a row, and update it, it updates the first row of the table, not the row i selected (third row down for example).
I''ve posted the code snippets of what I think are important.
private void Page_Loaded(object sender, RoutedEventArgs e)
{
DBEntities context = new DBEntities();
var orgTypeDetails = (from o in context.OrganisationTypeDetails
select o).ToList();
dgOrgTypeDetail.ItemsSource = orgTypeDetails;
}
private void btnUpdateOrgTypeDetail_Click(object sender, RoutedEventArgs e)
{
OrganisationTypeDetail selected = dgOrgTypeDetail.SelectedItem as OrganisationTypeDetail;
if (selected == null)
MessageBox.Show("You must select a 'Type' before updating.");
else
{
OrganisationTypeDetailUpdateView update = new OrganisationTypeDetailUpdateView();
update.ShowDialog();
Page_Loaded(null, null);
}
}
任何帮助将不胜感激.
非常感谢,格雷格.
Any help would be much appreciated.
Many thanks, Greg.
推荐答案
它应该可以工作.我可能建议的是处理SelectionChanged事件.然后,您可以使用更改后的值更新文本框,然后查看发生了什么情况.当您单击文本框时,可能会丢失所选的项目.
It should work. What I might suggest is to handle the SelectionChanged event. Then you can update a textbox with a value when it changes, and see what is happening. Might be loosing the selected item when you click on the text box.
这篇关于使用EF更新SelectedItem-WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!