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)
69 if (table.TableName == null || table.TableName == string.Empty)
73 table.dataSet = dataSet;
74 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, table));
77 public virtual DataTable Add (string name)
79 DataTable table = new DataTable (name);
84 public void AddRange (DataTable[] tables)
86 foreach (DataTable table in tables)
91 public bool CanRemove (DataTable table)
93 throw new NotImplementedException ();
101 public bool Contains (string name)
103 return (-1 != IndexOf (name, false));
106 public virtual int IndexOf (DataTable table)
108 return list.IndexOf (table);
111 public virtual int IndexOf (string name)
113 return IndexOf (name, false);
116 public void Remove (DataTable table)
118 this.Remove (table.TableName);
119 OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, table));
122 public void Remove (string name)
124 list.Remove (this [name]);
127 public void RemoveAt (int index)
129 list.RemoveAt (index);
134 #region Protected methods
136 protected internal virtual void OnCollectionChanging (CollectionChangeEventArgs Args)
138 if (CollectionChanging != null)
139 CollectionChanging (this, Args);
142 protected virtual void OnCollectionChanged (CollectionChangeEventArgs Args)
144 if (CollectionChanged != null)
145 CollectionChanged (this, Args);
150 #region Private methods
152 private int IndexOf (string name, bool error)
154 int count = 0, match = -1;
155 for (int i = 0; i < list.Count; i++)
157 String name2 = ((DataTable) list[i]).TableName;
158 if (String.Compare (name, name2, true) == 0)
160 if (String.Compare (name, name2, false) == 0)
168 if (count > 1 && error)
169 throw new ArgumentException ("There is no match for the name in the same case and there are multiple matches in different case.");
174 /// gives name to Table (Table1, Table2, Table3,...)
176 private void NameTable (DataTable Table)
178 string Name = "Table";
180 while (Contains (Name + i))
183 Table.TableName = Name + i;
186 #endregion // Private methods
190 [ResDescriptionAttribute ("Occurs whenever this collection's membership changes.")]
191 public event CollectionChangeEventHandler CollectionChanged;
193 public event CollectionChangeEventHandler CollectionChanging;