private void AddChangedRow (Hashtable addedRows, DataTable copyTable, DataRow row) { if (addedRows.ContainsKey (row)) return; foreach (DataRelation relation in row.Table.ParentRelations) { DataRow parent = ( row.RowState != DataRowState.Deleted ? row.GetParentRow (relation) : row.GetParentRow (relation, DataRowVersion.Original) ); if (parent == null) continue; // add the parent row DataTable parentCopyTable = copyTable.DataSet.Tables [parent.Table.TableName]; AddChangedRow (addedRows, parentCopyTable, parent); } // add the current row DataRow newRow = copyTable.NewNotInitializedRow (); copyTable.Rows.AddInternal (newRow); // Don't check for ReadOnly, when cloning data to new uninitialized row. row.CopyValuesToRow (newRow, false); newRow.XmlRowID = row.XmlRowID; addedRows.Add (row, row); }
// merge a row into a target table. private static void MergeRow(DataTable targetTable, DataRow row, bool preserveChanges) { DataColumnCollection columns = row.Table.Columns; DataColumn[] primaryKeys = targetTable.PrimaryKey; DataRow targetRow = null; DataRowVersion version = DataRowVersion.Default; if (row.RowState == DataRowState.Deleted) version = DataRowVersion.Original; if (primaryKeys != null && primaryKeys.Length > 0) // if there are any primary key. { // initiate an array that has the values of the primary keys. object[] keyValues = new object[primaryKeys.Length]; for (int j = 0; j < keyValues.Length; j++) { keyValues[j] = row[primaryKeys[j].ColumnName, version]; } // find the row in the target table. targetRow = targetTable.Rows.Find(keyValues, DataViewRowState.OriginalRows); if (targetRow == null) targetRow = targetTable.Rows.Find(keyValues); } // row doesn't exist in target table, or there are no primary keys. // create new row and copy values from source row to the new row. if (targetRow == null) { DataRow newRow = targetTable.NewNotInitializedRow(); row.CopyValuesToRow(newRow); targetTable.Rows.AddInternal (newRow); } // row exists in target table, and presere changes is false - // change the values of the target row to the values of the source row. else if (!preserveChanges) { row.CopyValuesToRow(targetRow); } }
private void AddChangedRow (Hashtable addedRows, DataTable copyTable, DataRow row) { if (addedRows.ContainsKey (row)) return; foreach (DataRelation relation in row.Table.ParentRelations) { DataRow parent = row.GetParentRow (relation); if (parent == null) continue; // add the parent row DataTable parentCopyTable = copyTable.DataSet.Tables [parent.Table.TableName]; AddChangedRow (addedRows, parentCopyTable, parent); } // add the current row DataRow newRow = copyTable.NewNotInitializedRow(); copyTable.Rows.AddInternal(newRow); row.CopyValuesToRow (newRow); newRow.XmlRowID = row.XmlRowID; addedRows.Add (row, row); }