2010-04-26 Ivan Zlatev <ivan@ivanz.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridViewCellCollection.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Pedro Martínez Juliá <pedromj@gmail.com>
24 //
25
26 #if NET_2_0
27
28 using System.Collections;
29 using System.ComponentModel;
30
31 namespace System.Windows.Forms
32 {
33         [ListBindable (false)]
34         public class DataGridViewCellCollection : BaseCollection, IList, ICollection, IEnumerable
35         {
36                 private DataGridViewRow dataGridViewRow;
37
38                 public DataGridViewCellCollection (DataGridViewRow dataGridViewRow) : base()
39                 {
40                         this.dataGridViewRow = dataGridViewRow;
41                 }
42
43                 bool IList.IsFixedSize {
44                         get { return base.List.IsFixedSize; }
45                 }
46
47                 object IList.this [int index] {
48                         get { return this [index]; }
49                         set { this [index] = value as DataGridViewCell; }
50                 }
51
52                 public DataGridViewCell this [int index] {
53                         get { return (DataGridViewCell) base.List [index]; }
54                         set {
55                                 if (value == null)
56                                         throw new ArgumentNullException ("value");
57
58                                 Insert(index, value);
59                         }
60                 }
61
62                 internal DataGridViewCell GetCellInternal (int colIndex)
63                 {
64                         return (DataGridViewCell) base.List [colIndex];
65                 }
66                 
67                 public DataGridViewCell this [string columnName] {
68                         get {
69                                 if (columnName == null)
70                                         throw new ArgumentNullException ("columnName");
71
72                                 foreach (DataGridViewCell cell in base.List) {
73                                         if (string.Compare (cell.OwningColumn.Name, columnName, true) == 0)
74                                                 return cell;
75                                 }
76
77                                 throw new ArgumentException (string.Format (
78                                         "Column name {0} cannot be found.",
79                                         columnName), "columnName");
80                         }
81                         set {
82                                 if (columnName == null)
83                                         throw new ArgumentNullException ("columnName");
84                                 if (value == null)
85                                         throw new ArgumentNullException ("value");
86
87                                 for (int i = 0; i < base.List.Count; i++) {
88                                         DataGridViewCell cell = (DataGridViewCell) base.List [i];
89                                         if (string.Compare (cell.OwningColumn.Name, columnName, true) == 0) {
90                                                 Insert (i, value);
91                                                 return;
92                                         }
93                                 }
94                                 Add (value);
95                         }
96                 }
97
98                 internal DataGridViewCell GetBoundCell (string dataPropertyName)
99                 {
100                         foreach (DataGridViewCell cell in base.List) {
101                                 if (string.Compare (cell.OwningColumn.DataPropertyName, dataPropertyName, true) == 0)
102                                         return cell;
103                         }
104                         
105                         return null;
106                 }
107                 
108                 public event CollectionChangeEventHandler CollectionChanged;
109
110                 int IList.Add (object value)
111                 {
112                         return Add (value as DataGridViewCell);
113                 }
114
115                 public virtual int Add (DataGridViewCell dataGridViewCell)
116                 {
117                         int result = base.List.Add (dataGridViewCell);
118                         dataGridViewCell.SetOwningRow (dataGridViewRow);
119                         dataGridViewCell.SetColumnIndex (result);
120                         dataGridViewCell.SetDataGridView (dataGridViewRow.DataGridView);
121                         OnCollectionChanged (new CollectionChangeEventArgs (
122                                 CollectionChangeAction.Add, dataGridViewCell));
123                         return result;
124                 }
125
126                 internal void Replace (int columnIndex, DataGridViewCell dataGridViewCell)
127                 {
128                         RemoveAt (columnIndex);
129                         Insert (columnIndex, dataGridViewCell);
130                 }
131
132                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
133                 public virtual void AddRange (params DataGridViewCell[] dataGridViewCells)
134                 {
135                         foreach (DataGridViewCell cell in dataGridViewCells)
136                                 Add (cell);
137                 }
138
139                 public virtual void Clear ()
140                 {
141                         base.List.Clear();
142                 }
143
144                 bool IList.Contains (object value)
145                 {
146                         return Contains (value as DataGridViewCell);
147                 }
148
149                 public virtual bool Contains (DataGridViewCell dataGridViewCell)
150                 {
151                         return base.List.Contains (dataGridViewCell);
152                 }
153
154                 public void CopyTo (DataGridViewCell[] array, int index)
155                 {
156                         base.List.CopyTo (array, index);
157                 }
158
159                 int IList.IndexOf (object value)
160                 {
161                         return IndexOf (value as DataGridViewCell);
162                 }
163
164                 public int IndexOf (DataGridViewCell dataGridViewCell)
165                 {
166                         return base.List.IndexOf (dataGridViewCell);
167                 }
168
169                 void IList.Insert (int index, object value)
170                 {
171                         Insert (index, value as DataGridViewCell);
172                 }
173
174                 public virtual void Insert (int index, DataGridViewCell dataGridViewCell)
175                 {
176                         base.List.Insert (index, dataGridViewCell);
177                         dataGridViewCell.SetOwningRow (dataGridViewRow);
178                         dataGridViewCell.SetColumnIndex (index);
179                         dataGridViewCell.SetDataGridView (dataGridViewRow.DataGridView);
180                         OnCollectionChanged (new CollectionChangeEventArgs (
181                                 CollectionChangeAction.Add, dataGridViewCell));
182                 }
183
184                 void IList.Remove (object value)
185                 {
186                         Remove (value as DataGridViewCell);
187                 }
188
189                 public virtual void Remove (DataGridViewCell cell)
190                 {
191                         base.List.Remove (cell);
192                         ReIndex ();
193                         OnCollectionChanged (new CollectionChangeEventArgs (
194                                 CollectionChangeAction.Remove, cell));
195                 }
196
197                 public virtual void RemoveAt (int index)
198                 {
199                         DataGridViewCell cell = this [index];
200                         base.List.RemoveAt (index);
201                         ReIndex ();
202                         OnCollectionChanged (new CollectionChangeEventArgs (
203                                 CollectionChangeAction.Remove, cell));
204                 }
205
206                 private void ReIndex ()
207                 {
208                         for (int i = 0; i < base.List.Count; i++)
209                                 this[i].SetColumnIndex (i);
210                 }
211
212                 protected override ArrayList List {
213                         get { return base.List; }
214                 }
215
216                 protected void OnCollectionChanged (CollectionChangeEventArgs e)
217                 {
218                         if (CollectionChanged != null) {
219                                 CollectionChanged(this, e);
220                         }
221                 }
222         }
223 }
224
225 #endif