如何使用LINQ-to-SQL回滚对WPF DataGrid控件的更改? [英] How to rollback changes to WPF DataGrid control using LINQ-to-SQL?

查看:121
本文介绍了如何使用LINQ-to-SQL回滚对WPF DataGrid控件的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够设置WPF Datagrid,通过linq-to-sql显示Northwind数据库表,并处理TheDataGrid_RowEditEnding事件,以便将其保存回数据库.

I was able to set up a WPF Datagrid, display a Northwind database table via linq-to-sql, and handle the TheDataGrid_RowEditEnding event so that it saves back the the database.

但是,当更改CustomerID时,它会从我处理的数据库中获取一个错误,但是现在我该如何(1)回滚Datagrid控件或(2)从数据库视图重新获取原始数据LINQ-to-SQL (我下面通过LINQ进行的重新引用似乎具有某种缓存,但不会刷新):

However, when a CustomerID was changed, it gets an error from the database which I handle, but how do I now either (1) rollback the Datagrid control or (2) refetch the original data from the database view LINQ-to-SQL (the refetching that I do below via LINQ seems to have some kind of caching, it doesn't refresh):

<Grid DockPanel.Dock="Bottom">
    <toolkit:DataGrid x:Name="TheDataGrid" 
                      AutoGenerateColumns="True"
                      RowEditEnding="TheDataGrid_RowEditEnding"/>
</Grid>



private void TheDataGrid_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
    try
    {
        _db.SubmitChanges();
    }
    catch (Exception ex)
    {
        RefreshData();
        Message.Text = ex.Message;
    }
}

public void RefreshData()
{
    var customers = from c in _db.Customers
                    select c;
    TheDataGrid.ItemsSource = customers;
}

答案:

感谢丹尼斯,我根据您的建议得到了我想要的:

ANSWER:

Thanks Denis, I used your suggestions to get what I was after:

private void TheDataGrid_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
    try
    {
        _db.SubmitChanges();
    }
    catch (Exception ex)
    {
        Customer customer = e.Row.Item as Customer;
        _db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, customer);
        Message.Text = ex.Message;
    }
}

推荐答案

首先,是否可以更改CustomerID?如果那会产生错误,我可以想象不是这样,那么您应该将该列标记为ReadOnly(我相信IsReadonly ="True").

First off, is it a requirement to be able to change the CustomerID? If that generates an error I would imagine that is not the case, so you should have that column marked as ReadOnly (IsReadonly="True" I believe).

第二,如果需要从数据库刷新对象,则需要在数据上下文上调用Refresh(),并将对象作为参数传递.这将从数据库中拉回当前值.

Second, if you need to refresh an object from the database, you need to call the Refresh() on the datacontext, passing your object as a parameter. This will pull back the current values from the database.

第三,为了处理整个对象的编辑和回滚,DataGrid支持IEditableObject接口,该接口允许对象公开类似于本地事务的方法(基本上,只要调用BeginEdit,它都会对数据进行内部复制,并且可以还原如果需要,请进行更改).这全都是手动过程,但是如果我没记错的话,它是Windows窗体中已经存在很长时间的界面,您应该能够找到关于它的大量信息(或者比我更精通的人可以在此提供信息)给你一些例子.

Third, to deal with edit and rollbacks on whole object, the DataGrid supports the IEditableObject interface, which allows an object to expose local transaction-like methods (basically it makes internal copies of the data whenever BeginEdit is called and can revert the changes if needed). This is all a manual process, but it is an interface that has existed for a long time in Windows Forms if I am not mistaken, and you should be able to find tons of information about it (or someone better versed than me here can give you some examples).

您可以在对象上实现该接口(通过局部类,因为您的对象是LinqToSQL类,大概是设计者生成的).

You could implement that interface on your objects (through partial classes, since your objects are LinqToSQL classes, presumably generated by the designer).

此处是有关DataGrid及其相关选项的文章 .它有些旧,但是可能会给您更多的指示.

Here's an article about the DataGrid and some options you have with it. It is a bit old, but it might give you some more pointers. Here is another, shorter one.

顺便说一句,再次查看 WPF工具包,他们刚刚发布了新版本(3月2009版),它可能比以前更支持版本/验证.

By the way, check out the WPF toolkit again, they just released a new version (March 2009 version) which might have more support for edition/validation than it used too.

这篇关于如何使用LINQ-to-SQL回滚对WPF DataGrid控件的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆