OnRemoveColumn() protected method

Notifies the that a is being removed.
protected OnRemoveColumn ( DataColumn column ) : void
column DataColumn
return void
Ejemplo n.º 1
0
        /// <summary>
        /// Removes the specified DataColumn object from the collection.
        /// </summary>
        /// <param name="column">The DataColumn to remove.</param>
        public void Remove(DataColumn column)
        {
            if (column == null)
            {
                throw new ArgumentNullException("column", "'column' argument cannot be null.");
            }

            if (!Contains(column.ColumnName))
            {
                throw new ArgumentException("Cannot remove a column that doesn't belong to this table.");
            }

            string dependency = GetColumnDependency(column);

            if (dependency != String.Empty)
            {
                throw new ArgumentException("Cannot remove this column, because it is part of " + dependency);
            }

#if NOT_PFX
            CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Remove, column);
#endif

            int ordinal = column.Ordinal;
            UnregisterName(column.ColumnName);
            base.List.Remove(column);

            // Reset column info
            column.ResetColumnInfo();

            //Update the ordinals
            for (int i = ordinal; i < this.Count; i++)
#if NET_2_0
            { this[i].Ordinal = i; }
#else
            { this[i].SetOrdinal(i); }
#endif

            if (parentTable != null)
            {
                parentTable.OnRemoveColumn(column);
            }

            if (column.AutoIncrement)
            {
                autoIncrement.Remove(column);
            }

            column.PropertyChanged -= new PropertyChangedEventHandler(ColumnPropertyChanged);

#if NOT_PFX
            OnCollectionChanged(e);
#endif
        }
Ejemplo n.º 2
0
        internal bool CanRemove(DataColumn column, bool fThrowException)
        {
            if (column == null)
            {
                if (!fThrowException)
                {
                    return(false);
                }
                else
                {
                    throw ExceptionBuilder.ArgumentNull("column");
                }
            }
            if (column.table != table)
            {
                if (!fThrowException)
                {
                    return(false);
                }
                else
                {
                    throw ExceptionBuilder.CannotRemoveColumn();
                }
            }

            // allow subclasses to complain first.
            table.OnRemoveColumn(column);

            // We need to make sure the column is not involved in any Relations or Constriants
            if (table.primaryKey != null && table.primaryKey.Key.ContainsColumn(column))
            {
                if (!fThrowException)
                {
                    return(false);
                }
                else
                {
                    throw ExceptionBuilder.CannotRemovePrimaryKey();
                }
            }
            for (int i = 0; i < table.ParentRelations.Count; i++)
            {
                if (table.ParentRelations[i].ChildKey.ContainsColumn(column))
                {
                    if (!fThrowException)
                    {
                        return(false);
                    }
                    else
                    {
                        throw ExceptionBuilder.CannotRemoveChildKey(table.ParentRelations[i].RelationName);
                    }
                }
            }
            for (int i = 0; i < table.ChildRelations.Count; i++)
            {
                if (table.ChildRelations[i].ParentKey.ContainsColumn(column))
                {
                    if (!fThrowException)
                    {
                        return(false);
                    }
                    else
                    {
                        throw ExceptionBuilder.CannotRemoveChildKey(table.ChildRelations[i].RelationName);
                    }
                }
            }
            for (int i = 0; i < table.Constraints.Count; i++)
            {
                if (table.Constraints[i].ContainsColumn(column))
                {
                    if (!fThrowException)
                    {
                        return(false);
                    }
                    else
                    {
                        throw ExceptionBuilder.CannotRemoveConstraint(table.Constraints[i].ConstraintName, table.Constraints[i].Table.TableName);
                    }
                }
            }
            if (table.DataSet != null)
            {
                for (ParentForeignKeyConstraintEnumerator en = new ParentForeignKeyConstraintEnumerator(table.DataSet, table); en.GetNext();)
                {
                    Constraint constraint = en.GetConstraint();
                    if (((ForeignKeyConstraint)constraint).ParentKey.ContainsColumn(column))
                    {
                        if (!fThrowException)
                        {
                            return(false);
                        }
                        else
                        {
                            throw ExceptionBuilder.CannotRemoveConstraint(constraint.ConstraintName, constraint.Table.TableName);
                        }
                    }
                }
            }

            for (int i = 0; i < ColumnQueue.columnCount; i++)
            {
                DataColumn col = ColumnQueue.columns[i];
                if (fInClear && (col.Table == table || col.Table == null))
                {
                    continue;
                }
                Debug.Assert(col.Computed, "invalid (non an expression) column in the expression column queue");
                DataExpression expr = col.DataExpression;
                if (expr.DependsOn(column))
                {
                    if (!fThrowException)
                    {
                        return(false);
                    }
                    else
                    {
                        throw ExceptionBuilder.CannotRemoveExpression(col.ColumnName, col.Expression);
                    }
                }
            }

            return(true);
        }