From b2340c921bf9b15255e77550ccbf15034592d970 Mon Sep 17 00:00:00 2001 From: Christopher Podurgiel Date: Mon, 11 Feb 2002 21:50:47 +0000 Subject: * DataColumnChangeEventArgs.cs : Added * DataColumnChangeEventHandler.cs : Added svn path=/trunk/mcs/; revision=2336 --- mcs/class/System.Data/ChangeLog | 7 +- .../System.Data/DataColumnChangeEventArgs.cs | 80 ++++ .../System.Data/DataColumnCollection.cs | 413 +++++++++++++++++++++ 3 files changed, 499 insertions(+), 1 deletion(-) create mode 100644 mcs/class/System.Data/System.Data/DataColumnChangeEventArgs.cs create mode 100644 mcs/class/System.Data/System.Data/DataColumnCollection.cs diff --git a/mcs/class/System.Data/ChangeLog b/mcs/class/System.Data/ChangeLog index a819ee6c8e6..427c3eaee97 100644 --- a/mcs/class/System.Data/ChangeLog +++ b/mcs/class/System.Data/ChangeLog @@ -1,4 +1,9 @@ 2002-02-10 Christopher Podurgiel * Removed *.cs from System.Data as the correct files are in mcs/class/System.Data/System.Data - * Updated all Enums, Interfaces, and Delegates in System.Data \ No newline at end of file + * Updated all Enums, Interfaces, and Delegates in System.Data + +2002-02-11 Christopher Podurgiel + + * DataColumnChangeEventArgs.cs : Added + * DataColumnChangeEventHandler.cs : Added \ No newline at end of file diff --git a/mcs/class/System.Data/System.Data/DataColumnChangeEventArgs.cs b/mcs/class/System.Data/System.Data/DataColumnChangeEventArgs.cs new file mode 100644 index 00000000000..f79d27eaf1e --- /dev/null +++ b/mcs/class/System.Data/System.Data/DataColumnChangeEventArgs.cs @@ -0,0 +1,80 @@ +// +// System.Data.DataColumnChangeEventArgs.cs +// +// Author: +// Christopher Podurgiel (cpodurgiel@msn.com) +// +// (C) Chris Podurgiel +// + +using System; + +namespace System.Data +{ + /// + /// Provides data for the ColumnChanging event. + /// + public class DataColumnChangeEventArgs : EventArgs + { + + private DataColumn _column = null; + private DataRow _row = null; + private object _proposedValue = null; + + /// + /// Initializes a new instance of the DataColumnChangeEventArgs class. + /// + /// + /// + /// + public DataColumnChangeEventArgs(DataRow row, DataColumn column, object value) + { + _column = column; + _row = row; + _proposedValue = value; + } + + /// + /// Gets the DataColumn with a changing value. + /// + public DataColumn Column + { + get + { + return _column; + } + } + + + /// + /// Gets or sets the proposed new value for the column. + /// + public object ProposedValue + { + get + { + return _proposedValue; + } + set + { + _proposedValue = value; + } + } + + + /// + /// Gets the DataRow of the column with a changing value. + /// + public DataRow Row + { + get + { + return _row; + } + } + + + + + } +} diff --git a/mcs/class/System.Data/System.Data/DataColumnCollection.cs b/mcs/class/System.Data/System.Data/DataColumnCollection.cs new file mode 100644 index 00000000000..8fca31e70b8 --- /dev/null +++ b/mcs/class/System.Data/System.Data/DataColumnCollection.cs @@ -0,0 +1,413 @@ +// +// System.Data.DataColumnCollection.cs +// +// Author: +// Christopher Podurgiel (cpodurgiel@msn.com) +// +// (C) Chris Podurgiel +// + +using System; +using System.Collections; +using System.ComponentModel; + +namespace System.Data +{ + /// + /// Represents a collection of DataColumn objects for a DataTable. + /// + public class DataColumnCollection : InternalDataCollectionBase + { + + protected ArrayList list = null; + + // The defaultNameIndex is used to create a default name for a column if one wasn't given. + private int defaultNameIndex; + + //table should be the DataTable this DataColumnCollection belongs to. + private DataTable parentTable = null; + + // Internal Constructor. This Class can only be created from other classes in this assembly. + internal DataColumnCollection(DataTable table):base() + { + list = new ArrayList(); + defaultNameIndex = 1; + parentTable = table; + } + + + + /// + /// Gets the DataColumn from the collection at the specified index. + /// + public virtual DataColumn this[int index] + { + get + { + return (DataColumn)list[index]; + } + } + + /// + /// Gets the DataColumn from the collection with the specified name. + /// + public virtual DataColumn this[string name] + { + get + { + foreach (DataColumn column in list) + { + if (column.ColumnName == name) + { + return column; + } + } + + return null; + + } + } + + + /// + /// Gets a list of the DataColumnCollection items. + /// + protected override ArrayList List + { + get + { + return list; + } + } + + /// + /// Creates and adds a DataColumn object to the DataColumnCollection. + /// + /// + public virtual DataColumn Add() + { + DataColumn column = new DataColumn("Column" + defaultNameIndex.ToString()); + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Add, this); + list.Add(column); + OnCollectionChanged(e); + defaultNameIndex++; + return column; + } + + /// + /// Creates and adds the specified DataColumn object to the DataColumnCollection. + /// + /// The DataColumn to add. + public void Add(DataColumn column) + { + if(Contains(column.ColumnName)) + { + throw new DuplicateNameException("A column named " + column.ColumnName + " already belongs to this DataTable."); + } + else + { + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Add, this); + list.Add(column); + OnCollectionChanged(e); + return; + } + } + + /// + /// Creates and adds a DataColumn object with the specified name to the DataColumnCollection. + /// + /// The name of the column. + /// The newly created DataColumn. + public virtual DataColumn Add(string columnName) + { + + if (columnName == null || columnName == String.Empty) + { + columnName = "Column" + defaultNameIndex.ToString(); + defaultNameIndex++; + } + + if(Contains(columnName)) + { + throw new DuplicateNameException("A column named " + columnName + " already belongs to this DataTable."); + } + else + { + DataColumn column = new DataColumn(columnName); + + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Add, this); + list.Add(column); + OnCollectionChanged(e); + return column; + } + } + + /// + /// Creates and adds a DataColumn object with the specified name and type to the DataColumnCollection. + /// + /// The ColumnName to use when cretaing the column. + /// The DataType of the new column. + /// The newly created DataColumn. + public virtual DataColumn Add(string columnName, Type type) + { + if (columnName == null || columnName == "") + { + columnName = "Column" + defaultNameIndex.ToString(); + defaultNameIndex++; + } + + if(Contains(columnName)) + { + throw new DuplicateNameException("A column named " + columnName + " already belongs to this DataTable."); + } + else + { + DataColumn column = new DataColumn(columnName, type); + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Add, this); + list.Add(column); + OnCollectionChanged(e); + return column; + } + } + + /// + /// Creates and adds a DataColumn object with the specified name, type, and expression to the DataColumnCollection. + /// + /// The name to use when creating the column. + /// The DataType of the new column. + /// The expression to assign to the Expression property. + /// The newly created DataColumn. + public virtual DataColumn Add(string columnName, Type type, string expression) + { + if (columnName == null || columnName == "") + { + columnName = "Column" + defaultNameIndex.ToString(); + defaultNameIndex++; + } + + if(Contains(columnName)) + { + throw new DuplicateNameException("A column named " + columnName + " already belongs to this DataTable."); + } + else + { + DataColumn column = new DataColumn(columnName, type, expression); + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Add, this); + list.Add(column); + OnCollectionChanged(e); + return column; + } + } + + /// + /// Copies the elements of the specified DataColumn array to the end of the collection. + /// + /// The array of DataColumn objects to add to the collection. + public void AddRange(DataColumn[] columns) + { + foreach (DataColumn column in columns) + { + Add(column); + } + return; + } + + /// + /// Checks whether a given column can be removed from the collection. + /// + /// A DataColumn in the collection. + /// true if the column can be removed; otherwise, false. + public bool CanRemove(DataColumn column) + { + + //Check that the column does not have a null reference. + if (column == null) + { + return false; + } + + + //Check that the column is part of this collection. + if (!Contains(column.ColumnName)) + { + return false; + } + + + + //Check if this column is part of a relationship. (this could probably be written better) + foreach (DataRelation childRelation in parentTable.ChildRelations) + { + foreach (DataColumn childColumn in childRelation.ChildColumns) + { + if (childColumn == column) + { + return false; + } + } + + foreach (DataColumn parentColumn in childRelation.ParentColumns) + { + if (parentColumn == column) + { + return false; + } + } + } + + //Check if this column is part of a relationship. (this could probably be written better) + foreach (DataRelation parentRelation in parentTable.ParentRelations) + { + foreach (DataColumn childColumn in parentRelation.ChildColumns) + { + if (childColumn == column) + { + return false; + } + } + + foreach (DataColumn parentColumn in parentRelation.ParentColumns) + { + if (parentColumn == column) + { + return false; + } + } + } + + + //Check if another column's expression depends on this column. + + foreach (DataColumn dataColumn in list) + { + if (dataColumn.Expression.ToString().IndexOf(column.ColumnName) > 0) + { + return false; + } + } + + + return true; + } + + /// + /// Clears the collection of any columns. + /// + public void Clear() + { + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Refresh, this); + list.Clear(); + OnCollectionChanged(e); + return; + } + + /// + /// Checks whether the collection contains a column with the specified name. + /// + /// The ColumnName of the column to check for. + /// true if a column exists with this name; otherwise, false. + public bool Contains(string name) + { + return (IndexOf(name) != -1); + } + + /// + /// Gets the index of a column specified by name. + /// + /// The name of the column to return. + /// The index of the column specified by column if it is found; otherwise, -1. + public virtual int IndexOf(DataColumn column) + { + return list.IndexOf(column); + } + + /// + /// Gets the index of the column with the given name (the name is not case sensitive). + /// + /// The name of the column to find. + /// The zero-based index of the column with the specified name, or -1 if the column doesn't exist in the collection. + public int IndexOf(string columnName) + { + + DataColumn column = this[columnName]; + + if (column != null) + { + return IndexOf(column); + } + else + { + return -1; + } + } + + /// + /// Raises the OnCollectionChanged event. + /// + /// A CollectionChangeEventArgs that contains the event data. + protected virtual void OnCollectionChanged(CollectionChangeEventArgs ccevent) + { + if (CollectionChanged != null) + { + // Invokes the delegate. + CollectionChanged(this, ccevent); + } + } + + /// + /// Raises the OnCollectionChanging event. + /// + /// A CollectionChangeEventArgs that contains the event data. + protected internal virtual void OnCollectionChanging(CollectionChangeEventArgs ccevent) + { + if (CollectionChanged != null) + { + // Invokes the delegate. + CollectionChanged(this, ccevent); + } + } + + /// + /// Removes the specified DataColumn object from the collection. + /// + /// The DataColumn to remove. + public void Remove(DataColumn column) + { + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Remove, this); + list.Remove(column); + OnCollectionChanged(e); + return; + } + + /// + /// Removes the DataColumn object with the specified name from the collection. + /// + /// The name of the column to remove. + public void Remove(string name) + { + DataColumn column = this[name]; + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Remove, this); + list.Remove(column); + OnCollectionChanged(e); + return; + } + + /// + /// Removes the column at the specified index from the collection. + /// + /// The index of the column to remove. + public void RemoveAt(int index) + { + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Remove, this); + list.RemoveAt(index); + OnCollectionChanged(e); + return; + } + + /// + /// Occurs when the columns collection changes, either by adding or removing a column. + /// + public event CollectionChangeEventHandler CollectionChanged; + + } +} -- cgit v1.2.3