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); }
/// <summary> /// Creates a row using specified values and adds it to the DataRowCollection. /// </summary> public DataRow Add(params object[] values) { if (values == null) { throw new NullReferenceException(); } DataRow row = table.NewNotInitializedRow(); int newRecord = table.CreateRecord(values); row.ImportRecord(newRecord); row.Validate(); AddInternal(row); return(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 { row.MergeValuesToRow(targetRow, preserveChanges); } }
// 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.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); }
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); }
public DataTable ToTable(string tablename, bool isDistinct, params string[] columnNames) { if (columnNames == null) { throw new ArgumentNullException("columnNames", "'columnNames' argument cannot be null."); } DataTable newTable = new DataTable(tablename); DataColumn[] columns; ListSortDirection[] sortDirection = null; if (columnNames.Length != 0) { columns = new DataColumn [columnNames.Length]; for (int i = 0; i < columnNames.Length; ++i) { columns [i] = Table.Columns [columnNames [i]]; } if (sortColumns != null) { sortDirection = new ListSortDirection [columnNames.Length]; for (int i = 0; i < columnNames.Length; ++i) { sortDirection [i] = ListSortDirection.Ascending; for (int j = 0; j < sortColumns.Length; ++j) { if (sortColumns [j] != columns [i]) { continue; } sortDirection [i] = sortOrder [j]; } } } } else { columns = (DataColumn[])Table.Columns.ToArray(typeof(DataColumn)); sortDirection = sortOrder; } ArrayList expressionCols = new ArrayList(); for (int i = 0; i < columns.Length; ++i) { DataColumn col = columns [i].Clone(); if (col.Expression != String.Empty) { col.Expression = ""; expressionCols.Add(col); } if (col.ReadOnly) { col.ReadOnly = false; } newTable.Columns.Add(col); } DataRow [] rows; Index index = new Index(new Key(Table, columns, sortDirection, RowStateFilter, rowFilterExpr)); if (isDistinct) { rows = index.GetDistinctRows(); } else { rows = index.GetAllRows(); } foreach (DataRow row in rows) { DataRow newRow = newTable.NewNotInitializedRow(); newTable.Rows.AddInternal(newRow); newRow.Original = -1; if (row.HasVersion(DataRowVersion.Current)) { newRow.Current = newTable.RecordCache.CopyRecord(Table, row.Current, -1); } else if (row.HasVersion(DataRowVersion.Original)) { newRow.Current = newTable.RecordCache.CopyRecord(Table, row.Original, -1); } foreach (DataColumn col in expressionCols) { newRow [col] = row [col.ColumnName]; } newRow.Original = -1; } return(newTable); }