2 // System.Data.DataTableCollection.cs
5 // Christopher Podurgiel (cpodurgiel@msn.com)
6 // Tim Coleman <tim@timcoleman.com>
9 // (C) Copyright 2002 Tim Coleman
13 using System.Collections;
14 using System.ComponentModel;
15 using System.Globalization;
17 namespace System.Data {
19 /// Represents the collection of tables for the DataSet.
22 [DefaultEvent ("CollectionChanged")]
23 [ListBindable (false)]
25 public class DataTableCollection : InternalDataCollectionBase
31 internal DataTableCollection (DataSet dataSet)
34 this.dataSet = dataSet;
41 public DataTable this[int index] {
42 get { return (DataTable)(list[index]); }
45 public DataTable this[string name] {
47 int index = IndexOf (name, true);
48 return index < 0 ? null : (DataTable) list[index];
52 protected override ArrayList List {
60 public virtual DataTable Add ()
62 DataTable Table = new DataTable ();
67 public virtual void Add (DataTable table)
70 // check if the reference is a null reference
72 throw new ArgumentNullException("table");
74 // check if the list already contains this tabe.
75 if(list.Contains(table))
76 throw new ArgumentException("DataTable already belongs to this DataSet.");
78 // if the table name is null or empty string.
80 if (table.TableName == null || table.TableName == string.Empty)
83 // check if the collection has a table with the same name.
84 if(Contains(table.TableName))
85 throw new DuplicateNameException("A DataTable named '" + table.TableName + "' already belongs to this DataSet.");
88 table.dataSet = dataSet;
89 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, table));
92 public virtual DataTable Add (string name)
94 DataTable table = new DataTable (name);
99 public void AddRange (DataTable[] tables)
101 foreach (DataTable table in tables)
106 public bool CanRemove (DataTable table)
108 return CanRemove(table, false);
116 public bool Contains (string name)
118 return (-1 != IndexOf (name, false));
121 public virtual int IndexOf (DataTable table)
123 return list.IndexOf (table);
126 public virtual int IndexOf (string name)
128 return IndexOf (name, false);
131 public void Remove (DataTable table)
133 CanRemove(table, true);
135 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, table));
138 public void Remove (string name)
140 Remove (this [name]);
143 public void RemoveAt (int index)
145 DataTable t = this[index];
147 list.RemoveAt (index);
148 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, t));
153 #region Protected methods
155 protected internal virtual void OnCollectionChanging (CollectionChangeEventArgs Args)
157 if (CollectionChanging != null)
158 CollectionChanging (this, Args);
161 protected virtual void OnCollectionChanged (CollectionChangeEventArgs Args)
163 if (CollectionChanged != null)
164 CollectionChanged (this, Args);
169 #region Private methods
171 private int IndexOf (string name, bool error)
173 int count = 0, match = -1;
174 for (int i = 0; i < list.Count; i++)
176 String name2 = ((DataTable) list[i]).TableName;
177 if (String.Compare (name, name2, true) == 0)
179 if (String.Compare (name, name2, false) == 0)
187 if (count > 1 && error)
188 throw new ArgumentException ("There is no match for the name in the same case and there are multiple matches in different case.");
193 /// gives name to Table (Table1, Table2, Table3,...)
195 private void NameTable (DataTable Table)
197 string Name = "Table";
199 while (Contains (Name + i))
202 Table.TableName = Name + i;
205 // check if a table can be removed from this collectiuon.
206 // if the table can not be remved act according to throwException parameter.
207 // if it is true throws an Exception, else return false.
208 private bool CanRemove(DataTable table, bool throwException)
210 // check if table is null reference
214 throw new ArgumentNullException("table");
218 // check if the table has the same DataSet as this collection.
219 if(table.DataSet != this.dataSet)
222 throw new ArgumentException("Table " + table.TableName + " does not belong to this DataSet.");
226 // check the table has a relation attached to it.
227 if (table.ParentRelations.Count > 0 || table.ChildRelations.Count > 0)
230 throw new ArgumentException("Cannot remove a table that has existing relations. Remove relations first.");
235 // now we check if any ForeignKeyConstraint is referncing 'table'.
236 IEnumerator tableEnumerator = this.dataSet.Tables.GetEnumerator();
238 // loop on all tables in dataset
239 while (tableEnumerator.MoveNext())
241 IEnumerator constraintEnumerator = ((DataTable) tableEnumerator.Current).Constraints.GetEnumerator();
242 // loop on all constrains in the current table
243 while (constraintEnumerator.MoveNext())
245 Object o = constraintEnumerator.Current;
246 // we only check ForeignKeyConstraint
247 if (o is ForeignKeyConstraint)
249 ForeignKeyConstraint fc = (ForeignKeyConstraint) o;
250 if(fc.Table == table || fc.RelatedTable == table)
253 throw new ArgumentException("Cannot remove table " + table.TableName + ", because it referenced in ForeignKeyConstraint " + fc.ConstraintName + ". Remove the constraint first.");
263 #endregion // Private methods
267 [ResDescriptionAttribute ("Occurs whenever this collection's membership changes.")]
268 public event CollectionChangeEventHandler CollectionChanged;
270 public event CollectionChangeEventHandler CollectionChanging;