How to determine if a row in an ObservableCollection <T> has changed
I have an ObservableCollection
private static CertOrigin_Entities db = new CertOrigin_Entities (); private static ObservableCollection ocSHIPPING_DTL;
I have a Datagrid WPF that I associate with late binding
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(); } } I update the SQL server database behind the button and close the window
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's what happens to me ... In my datagrid I have a text box
<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> If the text field has 5864.193.45 as the value, and I change it to 5864.193.46 and then to 5864.193.45, the ObservableCollection saves the record as the record changes, however I do not want it to save the record as it has not changed.
The main issue of change is this. I have three triggers on the table
1 - after starting the update trigger, which removes and inserts it into another database
2 - after launching the update trigger, which sends an email to several people indicating that the user has changed the data, and they need to retype the documents and send them to customs
3 - Enable the update trigger, which updates another table and sets the last_used_tarrif field to the value that the user has changed.
If the user has not actually changed the data, none of the above actions should be triggered. Is there a way to check if the record has really changed?
Is it possible to compare the records in the observed collection with the records in the table and update only those that actually differ?
Any tips?
Since you have an UpdateSourceTrigger set to PropertyChanged (see MSDN: Binding.UpdateSourceTrigger , every time it detects a change, it will act with the change.
Changing from 5864.193.45 to 5864.193.46 is one action, then 5864.193.46-56464.193.45 is another action.
Process 1: You can also change UpdateSourceTrigger to LostFocus ( UpdateSourceTrigger=LostFocus ), this makes the changes not work until you click elsewhere. Thus, if you change the value from 5864.193.45 to 5864.193.46, then from 5864.193.46 to 5864.193.45 it will not affect the change until you leave this text area.
Process 2: You can always implement the ObservationCollection wizard, and then compare only when _SaveChanges () is called.
Ok found an interesting item
private void dg_VQ_TABLE_VIEW_FocusedRowChanged(object sender, DevExpress.Xpf.Grid.FocusedRowChangedEventArgs e) { int rHANDLE = dg_VQ_TABLE_VIEW.FocusedRowHandle; //if (rHANDLE == dg_VQ_DTL_TABLE_VIEW.NewItemRowData.RowHandle.Value) { return; } _sync_child(rHANDLE); } When I select an item in the list, the tabular view of the data grid causes a row change.
So, I added a boolean and now the problem is gone.
private void lst_REQ_LIST_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (lst_REQ_LIST.SelectedValue == null) { return; } _selection_changed(); bln_CHANGING_REQ = false; } private void _selection_changed() { string sGUID = lst_REQ_LIST.SelectedValue.ToString().ToUpper(); req_guid = new Guid(sGUID); quote_guid = new Guid("{00000000-0000-0000-0000-000000000000}"); bln_CHANGING_REQ = true; _load_data(); } private void dg_VQ_TABLE_VIEW_FocusedRowChanged(object sender, DevExpress.Xpf.Grid.FocusedRowChangedEventArgs e) { if (bln_CHANGING_REQ) { return; } int rHANDLE = dg_VQ_TABLE_VIEW.FocusedRowHandle; //if (rHANDLE == dg_VQ_DTL_TABLE_VIEW.NewItemRowData.RowHandle.Value) { return; } _sync_child(rHANDLE); }