/// <summary> /// Applies all given transactions /// </summary> /// <param name="ops">Operation list</param> private void ApplyTransaction(List <UndoRedoOperationBase> ops) { int i = 0; while (i < ops.Count) { UndoRedoOperationBase r = ops[i]; DataRow row = r.Row; switch (r.OperationType) { case UndoRedoOperationBaseType.NewRow: UndoRedoRowOperation rowOp = (UndoRedoRowOperation)r; DataTable sourceTable = rowOp.Table; DataRow newRow = sourceTable.NewRow(); if (rowOp.WasDeleted) { RestoreRowFields(rowOp, newRow); } bool flag = true; int j = i + 1; while (flag & j < ops.Count) { if (ops[j].OperationType == UndoRedoOperationBaseType.ChangeField) { UndoRedoFieldOperation f = (UndoRedoFieldOperation)ops[j]; newRow[f.ColumnName] = f.NewValue; i = j - 1; } else { flag = false; } j++; } sourceTable.Rows.Add(newRow); for (int n = r.ID; n < m_operations.Count; ++n) { if (m_operations[n].Row == row) { m_operations[n].Row = newRow; } } break; case UndoRedoOperationBaseType.DeleteRow: //((UndoRedoRowOperation)r).Table.Rows.Remove(row); ((UndoRedoRowOperation)r).Row.Delete(); break; case UndoRedoOperationBaseType.ChangeField: UndoRedoFieldOperation fieldOp = (UndoRedoFieldOperation)r; row[fieldOp.ColumnName] = fieldOp.NewValue; break; } i++; } }
/// <summary> /// Restores all row fields saved in the transaction record's field-value collection. /// </summary> /// <param name="record"></param> /// <param name="row"></param> private void RestoreRowFields(UndoRedoRowOperation record, DataRow row) { foreach (DataColumn dc in row.Table.Columns) { row[dc] = record.GetValue(dc.ColumnName); } }
/// <summary> /// Saves all row fields to the transaction record's field-value collection. /// </summary> /// <param name="record"></param> /// <param name="row"></param> private void SaveRowFields(UndoRedoRowOperation record, DataRow row) { foreach (DataColumn dc in row.Table.Columns) { try { record.AddColumnNameValuePair(dc.ColumnName, row[dc]); } catch { } } }
/// <summary> /// The row deleting event fires when the row has being removed fro the collection. /// We can't use the row deleted event to record the row field values because the row /// has been then marked as deleted and accessing the fields throws an exception. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnRowDeleting(object sender, DataRowChangeEventArgs e) { if (m_doLogging) { UndoRedoRowOperation record = new UndoRedoRowOperation (m_operations.Count, e.Row.Table, e.Row, UndoRedoOperationBaseType.DeleteRow); if (m_trasaction) { record.TransactionId = curentTransactionID; } m_operations.Add(record); SaveRowFields(record, e.Row); OnRedoUndoOperationAdded(new UndoRedoEventArgs(record)); } }
/// <summary> /// Log the new row and add it to the uncommitted row collection. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnTableNewRow(object sender, DataTableNewRowEventArgs e) { if (m_doLogging) { int idx = m_operations.Count; UndoRedoRowOperation record = new UndoRedoRowOperation (idx, e.Row.Table, e.Row, UndoRedoOperationBaseType.NewRow); if (m_trasaction) { record.TransactionId = curentTransactionID; } OnRedoUndoOperationAdding(new UndoRedoEventArgs(record)); m_operations.Add(record); OnRedoUndoOperationAdded(new UndoRedoEventArgs(record)); List <int> rowIndices = new List <int> { idx }; m_uncomittedRows.Add(e.Row, rowIndices); } }