a77d641e827db65eb54aa3f29f9c9a54d8d78b9d
[mono.git] / mcs / class / System.Data / System.Data / DataTableCollection.cs
1 //
2 // System.Data.DataTableCollection.cs
3 //
4 // Authors:
5 //   Christopher Podurgiel (cpodurgiel@msn.com)
6 //   Tim Coleman <tim@timcoleman.com>
7 //
8 // (C) Chris Podurgiel
9 // (C) Copyright 2002 Tim Coleman
10 //
11
12 using System;
13 using System.Collections;
14 using System.ComponentModel;
15 using System.Globalization;
16
17 namespace System.Data {
18         /// <summary>
19         /// Represents the collection of tables for the DataSet.
20         /// </summary>
21         [Editor]
22         [DefaultEvent ("CollectionChanged")]
23         [ListBindable (false)]
24         [Serializable]
25         public class DataTableCollection : InternalDataCollectionBase
26         {
27                 DataSet dataSet;
28                 
29                 #region Constructors 
30
31                 internal DataTableCollection (DataSet dataSet)
32                         : base ()
33                 {
34                         this.dataSet = dataSet;
35                 }
36                 
37                 #endregion
38                 
39                 #region Properties
40
41                 public DataTable this[int index] {
42                         get { return (DataTable)(list[index]); }
43                 }
44
45                 public DataTable this[string name] {
46                         get { 
47                                 int index = IndexOf (name, true);
48                                 return index < 0 ? null : (DataTable) list[index];
49                         }
50                 }
51
52                 protected override ArrayList List {
53                         get { return list; }
54                 }
55
56                 #endregion
57         
58                 #region Methods 
59
60                 public virtual DataTable Add () 
61                 {
62                         DataTable Table = new DataTable ();
63                         Add (Table);
64                         return Table;
65                 }
66
67                 public virtual void Add (DataTable table) 
68                 {
69                         if (table.TableName == null || table.TableName == string.Empty)
70                                 NameTable (table);
71                                 
72                         list.Add (table);
73                         table.dataSet = dataSet;
74                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, table));
75                 }
76
77                 public virtual DataTable Add (string name) 
78                 {
79                         DataTable table = new DataTable (name);
80                         this.Add (table);
81                         return table;
82                 }
83
84                 public void AddRange (DataTable[] tables) 
85                 {
86                         foreach (DataTable table in tables)
87                                 this.Add (table);
88                 }
89
90                 [MonoTODO]
91                 public bool CanRemove (DataTable table) 
92                 {
93                         throw new NotImplementedException ();
94                 }
95
96                 public void Clear () 
97                 {
98                         list.Clear ();
99                 }
100
101                 public bool Contains (string name) 
102                 {
103                         return (-1 != IndexOf (name, false));
104                 }
105
106                 public virtual int IndexOf (DataTable table) 
107                 {
108                         return list.IndexOf (table);
109                 }
110
111                 public virtual int IndexOf (string name) 
112                 {
113                         return IndexOf (name, false);
114                 }
115
116                 public void Remove (DataTable table) 
117                 {
118                         this.Remove (table.TableName);
119                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, table));
120                 }
121
122                 public void Remove (string name) 
123                 {
124                         list.Remove (this [name]);
125                 }
126
127                 public void RemoveAt (int index) 
128                 {
129                         list.RemoveAt (index);
130                 }
131
132                 #endregion
133
134                 #region Protected methods
135
136                 protected internal virtual void OnCollectionChanging (CollectionChangeEventArgs Args)
137                 {
138                         if (CollectionChanging != null)
139                                 CollectionChanging (this, Args);
140                 }
141
142                 protected virtual void OnCollectionChanged (CollectionChangeEventArgs Args)
143                 {
144                         if (CollectionChanged != null)
145                                 CollectionChanged (this, Args);
146                 }
147
148                 #endregion
149
150                 #region Private methods
151
152                 private int IndexOf (string name, bool error)
153                 {
154                         int count = 0, match = -1;
155                         for (int i = 0; i < list.Count; i++)
156                         {
157                                 String name2 = ((DataTable) list[i]).TableName;
158                                 if (String.Compare (name, name2, true) == 0)
159                                 {
160                                         if (String.Compare (name, name2, false) == 0)
161                                                 return i;
162                                         match = i;
163                                         count++;
164                                 }
165                         }
166                         if (count == 1)
167                                 return match;
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.");
170                         return -1;
171                 }
172
173                 /// <summary>
174                 /// gives name to Table (Table1, Table2, Table3,...)
175                 /// </summary>
176                 private void NameTable (DataTable Table)
177                 {
178                         string Name = "Table";
179                         int i = 1;
180                         while (Contains (Name + i))
181                                 i++;
182
183                         Table.TableName = Name + i;                            
184                 }
185
186                 #endregion // Private methods
187
188                 #region Events
189
190                 [ResDescriptionAttribute ("Occurs whenever this collection's membership changes.")]             
191                 public event CollectionChangeEventHandler CollectionChanged;
192
193                 public event CollectionChangeEventHandler CollectionChanging;
194
195                 #endregion
196         }
197 }