/// <summary> /// /// </summary> /// <param name="owner"></param> /// <param name="externalRow"></param> private void ImportIntoOwner(DataRow externalRow, DataItemCollection owner) { // If we have an owner and we're exporting an external row, set our row as a new row of the owner's table if (owner != null) { _owner = owner; _row = _owner.InnerTable.NewRow(); } // Set matching fields, check against the stand-alone item's fields for (int i = 0; i < this.InnerTable.Columns.Count; i++) { // Get the name of the field string fieldName = this.InnerTable.Columns[i].ColumnName; // Get the index of the field for this item's row, can be different from the stand-alone version we're checking against int currentRowIndex = _row.Table != this.InnerTable ? _row.Table.Columns.IndexOf(fieldName) : i; // Get the index of the field in the external row int externalRowIndex = externalRow.Table.Columns.IndexOf(fieldName); if (currentRowIndex > -1 && externalRowIndex > -1) { // Apply the data to the collection row only if both the internal row and the external row have field _row[currentRowIndex] = externalRow[externalRowIndex]; } else if (externalRowIndex > -1) { // Apply the data to the retrieved field row since we already have it if (_retrievedFieldRow == null) { // Create it if it's missing _retrievedFieldRow = this.InnerTable.NewRow(); this.InnerTable.Rows.Add(_retrievedFieldRow); } _retrievedFieldRow[i] = externalRow[externalRowIndex]; } else { // Otherwise mark it is as missing for RetrieveMissing to collect later on if necessary _missingFields.Add(fieldName); } } // This is a new addition June 3 2007 if (owner != null) { BackupFieldValues(); } }
internal void SetParentCollection(DataItemCollection owner) { if (owner == null) { throw new ArgumentNullException("owner"); } // Don't allow if item is already in collection-owned mode if (_owner != null) { throw new InvalidOperationException("Cannot change the parent collection of an item that already belongs to a different collection."); } // Import our own row as an external row specifying an owner ImportIntoOwner(this.Row, owner); }
/// <summary> /// Initializes an item with existing row data, either collection-based on independent mode. /// </summary> /// /// <param name="externalRow"> /// The row containing the item's data. /// </param> /// /// <param name="owner"> /// The collection that is creating the item. If null, the item is created as an independent item. /// </param> /// /// <param name="copyLocal"> /// If false, data is not copied into the internal table even if there is no owner specified. /// </param> /// /// <remarks> /// <para> /// If externalRow does not belong to the owner's InnerTable, the data will be imported into /// the owner's table as a new row (or imported into the item's independent table if no owner is specified). /// </para> /// <para> /// Fields that exist in the independent version of the item but do not exist in the external row will be /// marked as "missing" so that if they are referenced in code their value will be retrieved on-demand. /// </para> /// </remarks> protected internal DataItem(DataRow externalRow, DataItemCollection owner, bool copyLocal) : this() { // If no external row is specified, use a new empty row from the owner or the inner table if (externalRow == null) { externalRow = owner != null?owner.InnerTable.NewRow() : InnerTable.NewRow(); } // Collect missing column names _missingFields = new ArrayList(); // Row data needs to be imported since we're not attaching to a collection if (copyLocal && (owner == null || externalRow.Table != owner.InnerTable)) { ImportIntoOwner(externalRow, owner); } else { _row = externalRow; _owner = owner; // Look for missing fields foreach (DataColumn column in _table.Columns) { if (_row.Table.Columns.IndexOf(column.ColumnName) < 0) { _missingFields.Add(column.ColumnName); } } } if (!copyLocal || _owner != null) { // We don't need the table any more since we have an owner collection _table.Dispose(); _table = null; } }
/// <summary> /// /// </summary> /// <param name="externalRow"></param> /// <param name="owner"></param> protected internal DataItem(DataRow externalRow, DataItemCollection owner) : this(externalRow, owner, true) { }