如何确定ObservableCollection< T>中的行是否实际上改变了 [英] How to determine if a row in ObservableCollection<T> actually changed

查看:121
本文介绍了如何确定ObservableCollection< T>中的行是否实际上改变了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ObservableCollection

I have an ObservableCollection

私有静态CertOrigin_Entities db = new CertOrigin_Entities(); 私有静态ObservableCollection ocSHIPPING_DTL;

private static CertOrigin_Entities db = new CertOrigin_Entities(); private static ObservableCollection ocSHIPPING_DTL;

我有一个WPF Datagrid,我在以后进行绑定

I have a WPF Datagrid that I do late binding on

    private void btn_SEARCH_Click(object sender, RoutedEventArgs e)
    {
        string sCI = this.txt_SEARCH.Text;

        var sd = (db.TBL_SHIPPING.Where(x => x.CommercialInvoiceNumber == sCI)).ToList();
        if (sd.Count() > 0)
        {
            iID = (int)sd[0].SHIPPING_ID;
            var query = (db.v_wpf_cert_origin.Where(x => x.SHIPPING_ID == iID));
            ocSHIPPING_DTL = new ObservableCollection<v_wpf_cert_origin>(query.ToList());

            dgCOO.ItemsSource = ocSHIPPING_DTL;

            var cust = (from x in db.TBL_CUSTOMER
                     join y in db.TBL_REQUISITION on x.CUSTOMER_ID equals y.CUSTOMER_ID
                     join z in db.TBL_SHIPPING on y.REQ_ID equals z.REQ_ID
                     where z.SHIPPING_ID == iID
                     select new {CUST = x.CustomerName}).ToList();
            this.lbl_CUSTOMER.Content = cust[0].CUST.ToString();
        }
    }

我正在更新SQL Server数据库 在按钮后面并在窗口上关闭

I am updating the SQL server database behind a button and on the window close

    private static bool _SaveChanges()
    {
        DbTransaction _dbTransaction = null;
        db.Connection.Open();
        using (_dbTransaction = db.Connection.BeginTransaction())
        {
            try
            {
                db.SaveChanges();
                db.AcceptAllChanges();
                _dbTransaction.Commit();
                db.Connection.Close();
            }
            catch (TransactionAbortedException ex)
            {
                db.Connection.Close();
                throw ex;
            }
            return true;
        }
    }

这就是我正在发生的事情... 在我的数据网格中,我有一个文本框

Here is what is happening to me... In my datagrid, I have a textbox

                <DataGridTemplateColumn Header="Hs Tarriff Class #:" Width="125" IsReadOnly="False">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=HsTarriffClassNumber, 
                                UpdateSourceTrigger=PropertyChanged, 
                                NotifyOnTargetUpdated=True}" 
                                     Name="txt_HsTarriffClassNumber" 
                                      />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>

如果文本框的值为5864.193.45,而我将其更改为5864.193.46,然后又返回5864.193.45,则ObservableCollection将记录保存为更改的记录,但是我不希望它将记录另存为它没有改变.

If the text box has 5864.193.45 as the value and I change it to 5864.193.46 then back to 5864.193.45 the ObservableCollection saves the record as the record did change, however I do not want it to save the record as it did not change.

更改的主要问题是这个. 我桌上有3个触发器

The primary issue of the change is this. I have 3 triggers on the table

1-更新之后,审计触发器将删除的触发器插入到另一个数据库中

1 - After update Audit trigger that takes the deleted and inserts it into another database

2-在更新审核触发器之后,该触发器向许多人发送电子邮件,指出用户更改了数据,因此他们需要重新打印文书工作并将其发送给海关.

2 - After Update Audit trigger that sends an email to a multidude of people stating that the user changed the data and they need to reprint the paperwork and send it to customs

3-在更新"触发器上,该触发器将更新不同的表,并将last_used_tarrif字段设置为等于用户更改的值.

3 - On Update trigger that updates a different table and sets the last_used_tarrif field equal to the value the user changed.

如果用户未实际更改数据,则以上任何一项均不触发. 有没有办法检查记录是否实际更改?

If the user did not actually change the data none of the above should fire. Is there a way to check if the record actually changed?

我可以将可观察集合中的记录与表中的记录进行比较,而仅更新实际上不同的记录吗?

Can I compare the records in the observable collection to the records in the table and only update those that are actually different?

有什么建议吗?

推荐答案

因为您已将UpdateSourceTrigger设置为PropertyChanged(请参阅

Because you have the UpdateSourceTrigger set to PropertyChanged (see MSDN: Binding.UpdateSourceTrigger, every time it detects a change, it will action a change.

从5864.193.45更改为5864.193.46是一项操作,然后从5864.193.46更改为5864.193.45是另一项操作.

Changing from 5864.193.45 to 5864.193.46 is one action, then 5864.193.46 to 5864.193.45 is another action.

过程1: 您还可以将UpdateSourceTrigger更改为LostFocus(UpdateSourceTrigger=LostFocus),这样就可以使更改在单击其他位置之前不起作用.这样,当您从5864.193.45更改为5864.193.46,然后从5864.193.46更改为5864.193.45时,直到离开该文本区域,该更改才会生效.

Process 1: You could also change UpdateSourceTrigger to LostFocus (UpdateSourceTrigger=LostFocus), this makes it so that the change doesn't get actioned until you click somewhere else. This way when you change from 5864.193.45 to 5864.193.46 , then 5864.193.46 to 5864.193.45 it wouldn't action the change until you leave that text area.

过程2: 您总是可以实现一个主要的ObservationCollection,然后仅在调用_SaveChanges()时进行比较.

Process 2: You could always implement a master ObservationCollection and then only compare when you call _SaveChanges().

这篇关于如何确定ObservableCollection&lt; T&gt;中的行是否实际上改变了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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