diff options
author | Christopher Podurgiel <cpodu@mono-cvs.ximian.com> | 2002-02-12 00:50:47 +0300 |
---|---|---|
committer | Christopher Podurgiel <cpodu@mono-cvs.ximian.com> | 2002-02-12 00:50:47 +0300 |
commit | b2340c921bf9b15255e77550ccbf15034592d970 (patch) | |
tree | ccde6689fdbd36262e13991f99c2f81394edae68 | |
parent | 381389ed222d8fcad513e7981e59808d6d50492d (diff) |
* DataColumnChangeEventArgs.cs : Added
* DataColumnChangeEventHandler.cs : Added
svn path=/trunk/mcs/; revision=2336
-rw-r--r-- | mcs/class/System.Data/ChangeLog | 7 | ||||
-rw-r--r-- | mcs/class/System.Data/System.Data/DataColumnChangeEventArgs.cs | 80 | ||||
-rw-r--r-- | mcs/class/System.Data/System.Data/DataColumnCollection.cs | 413 |
3 files changed, 499 insertions, 1 deletions
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 <cpodurgiel@msn.com> * 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 <cpodurgiel@msn.com> + + * 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 +{ + /// <summary> + /// Provides data for the ColumnChanging event. + /// </summary> + public class DataColumnChangeEventArgs : EventArgs + { + + private DataColumn _column = null; + private DataRow _row = null; + private object _proposedValue = null; + + /// <summary> + /// Initializes a new instance of the DataColumnChangeEventArgs class. + /// </summary> + /// <param name="row"></param> + /// <param name="column"></param> + /// <param name="value"></param> + public DataColumnChangeEventArgs(DataRow row, DataColumn column, object value) + { + _column = column; + _row = row; + _proposedValue = value; + } + + /// <summary> + /// Gets the DataColumn with a changing value. + /// </summary> + public DataColumn Column + { + get + { + return _column; + } + } + + + /// <summary> + /// Gets or sets the proposed new value for the column. + /// </summary> + public object ProposedValue + { + get + { + return _proposedValue; + } + set + { + _proposedValue = value; + } + } + + + /// <summary> + /// Gets the DataRow of the column with a changing value. + /// </summary> + 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 +{ + /// <summary> + /// Represents a collection of DataColumn objects for a DataTable. + /// </summary> + 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; + } + + + + /// <summary> + /// Gets the DataColumn from the collection at the specified index. + /// </summary> + public virtual DataColumn this[int index] + { + get + { + return (DataColumn)list[index]; + } + } + + /// <summary> + /// Gets the DataColumn from the collection with the specified name. + /// </summary> + public virtual DataColumn this[string name] + { + get + { + foreach (DataColumn column in list) + { + if (column.ColumnName == name) + { + return column; + } + } + + return null; + + } + } + + + /// <summary> + /// Gets a list of the DataColumnCollection items. + /// </summary> + protected override ArrayList List + { + get + { + return list; + } + } + + /// <summary> + /// Creates and adds a DataColumn object to the DataColumnCollection. + /// </summary> + /// <returns></returns> + 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; + } + + /// <summary> + /// Creates and adds the specified DataColumn object to the DataColumnCollection. + /// </summary> + /// <param name="column">The DataColumn to add.</param> + 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; + } + } + + /// <summary> + /// Creates and adds a DataColumn object with the specified name to the DataColumnCollection. + /// </summary> + /// <param name="columnName">The name of the column.</param> + /// <returns>The newly created DataColumn.</returns> + 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; + } + } + + /// <summary> + /// Creates and adds a DataColumn object with the specified name and type to the DataColumnCollection. + /// </summary> + /// <param name="columnName">The ColumnName to use when cretaing the column.</param> + /// <param name="type">The DataType of the new column.</param> + /// <returns>The newly created DataColumn.</returns> + 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; + } + } + + /// <summary> + /// Creates and adds a DataColumn object with the specified name, type, and expression to the DataColumnCollection. + /// </summary> + /// <param name="columnName">The name to use when creating the column.</param> + /// <param name="type">The DataType of the new column.</param> + /// <param name="expression">The expression to assign to the Expression property.</param> + /// <returns>The newly created DataColumn.</returns> + 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; + } + } + + /// <summary> + /// Copies the elements of the specified DataColumn array to the end of the collection. + /// </summary> + /// <param name="columns">The array of DataColumn objects to add to the collection.</param> + public void AddRange(DataColumn[] columns) + { + foreach (DataColumn column in columns) + { + Add(column); + } + return; + } + + /// <summary> + /// Checks whether a given column can be removed from the collection. + /// </summary> + /// <param name="column">A DataColumn in the collection.</param> + /// <returns>true if the column can be removed; otherwise, false.</returns> + 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; + } + + /// <summary> + /// Clears the collection of any columns. + /// </summary> + public void Clear() + { + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Refresh, this); + list.Clear(); + OnCollectionChanged(e); + return; + } + + /// <summary> + /// Checks whether the collection contains a column with the specified name. + /// </summary> + /// <param name="name">The ColumnName of the column to check for.</param> + /// <returns>true if a column exists with this name; otherwise, false.</returns> + public bool Contains(string name) + { + return (IndexOf(name) != -1); + } + + /// <summary> + /// Gets the index of a column specified by name. + /// </summary> + /// <param name="column">The name of the column to return.</param> + /// <returns>The index of the column specified by column if it is found; otherwise, -1.</returns> + public virtual int IndexOf(DataColumn column) + { + return list.IndexOf(column); + } + + /// <summary> + /// Gets the index of the column with the given name (the name is not case sensitive). + /// </summary> + /// <param name="columnName">The name of the column to find.</param> + /// <returns>The zero-based index of the column with the specified name, or -1 if the column doesn't exist in the collection.</returns> + public int IndexOf(string columnName) + { + + DataColumn column = this[columnName]; + + if (column != null) + { + return IndexOf(column); + } + else + { + return -1; + } + } + + /// <summary> + /// Raises the OnCollectionChanged event. + /// </summary> + /// <param name="ccevent">A CollectionChangeEventArgs that contains the event data.</param> + protected virtual void OnCollectionChanged(CollectionChangeEventArgs ccevent) + { + if (CollectionChanged != null) + { + // Invokes the delegate. + CollectionChanged(this, ccevent); + } + } + + /// <summary> + /// Raises the OnCollectionChanging event. + /// </summary> + /// <param name="ccevent">A CollectionChangeEventArgs that contains the event data.</param> + protected internal virtual void OnCollectionChanging(CollectionChangeEventArgs ccevent) + { + if (CollectionChanged != null) + { + // Invokes the delegate. + CollectionChanged(this, ccevent); + } + } + + /// <summary> + /// Removes the specified DataColumn object from the collection. + /// </summary> + /// <param name="column">The DataColumn to remove.</param> + public void Remove(DataColumn column) + { + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Remove, this); + list.Remove(column); + OnCollectionChanged(e); + return; + } + + /// <summary> + /// Removes the DataColumn object with the specified name from the collection. + /// </summary> + /// <param name="name">The name of the column to remove.</param> + public void Remove(string name) + { + DataColumn column = this[name]; + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Remove, this); + list.Remove(column); + OnCollectionChanged(e); + return; + } + + /// <summary> + /// Removes the column at the specified index from the collection. + /// </summary> + /// <param name="index">The index of the column to remove.</param> + public void RemoveAt(int index) + { + CollectionChangeEventArgs e = new CollectionChangeEventArgs(CollectionChangeAction.Remove, this); + list.RemoveAt(index); + OnCollectionChanged(e); + return; + } + + /// <summary> + /// Occurs when the columns collection changes, either by adding or removing a column. + /// </summary> + public event CollectionChangeEventHandler CollectionChanged; + + } +} |