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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Pedro MartÃnez Juliá <pedromj@gmail.com>
28 using System.Collections;
29 using System.ComponentModel;
31 namespace System.Windows.Forms
33 [ListBindable (false)]
34 public class DataGridViewCellCollection : BaseCollection, IList, ICollection, IEnumerable
36 private DataGridViewRow dataGridViewRow;
38 public DataGridViewCellCollection (DataGridViewRow dataGridViewRow) : base()
40 this.dataGridViewRow = dataGridViewRow;
43 bool IList.IsFixedSize {
44 get { return base.List.IsFixedSize; }
47 object IList.this [int index] {
48 get { return this [index]; }
49 set { this [index] = value as DataGridViewCell; }
52 public DataGridViewCell this [int index] {
53 get { return (DataGridViewCell) base.List [index]; }
56 throw new ArgumentNullException ("value");
62 internal DataGridViewCell GetCellInternal (int colIndex)
64 return (DataGridViewCell) base.List [colIndex];
67 public DataGridViewCell this [string columnName] {
69 if (columnName == null)
70 throw new ArgumentNullException ("columnName");
72 foreach (DataGridViewCell cell in base.List) {
73 if (string.Compare (cell.OwningColumn.Name, columnName, true) == 0)
77 throw new ArgumentException (string.Format (
78 "Column name {0} cannot be found.",
79 columnName), "columnName");
82 if (columnName == null)
83 throw new ArgumentNullException ("columnName");
85 throw new ArgumentNullException ("value");
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) {
98 internal DataGridViewCell GetBoundCell (string dataPropertyName)
100 foreach (DataGridViewCell cell in base.List) {
101 if (string.Compare (cell.OwningColumn.DataPropertyName, dataPropertyName, true) == 0)
108 public event CollectionChangeEventHandler CollectionChanged;
110 int IList.Add (object value)
112 return Add (value as DataGridViewCell);
115 public virtual int Add (DataGridViewCell dataGridViewCell)
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));
126 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
127 public virtual void AddRange (params DataGridViewCell[] dataGridViewCells)
129 foreach (DataGridViewCell cell in dataGridViewCells)
133 public virtual void Clear ()
138 bool IList.Contains (object value)
140 return Contains (value as DataGridViewCell);
143 public virtual bool Contains (DataGridViewCell dataGridViewCell)
145 return base.List.Contains (dataGridViewCell);
148 public void CopyTo (DataGridViewCell[] array, int index)
150 base.List.CopyTo (array, index);
153 int IList.IndexOf (object value)
155 return IndexOf (value as DataGridViewCell);
158 public int IndexOf (DataGridViewCell dataGridViewCell)
160 return base.List.IndexOf (dataGridViewCell);
163 void IList.Insert (int index, object value)
165 Insert (index, value as DataGridViewCell);
168 public virtual void Insert (int index, DataGridViewCell dataGridViewCell)
170 base.List.Insert (index, dataGridViewCell);
171 dataGridViewCell.SetOwningRow (dataGridViewRow);
172 dataGridViewCell.SetColumnIndex (index);
173 dataGridViewCell.SetDataGridView (dataGridViewRow.DataGridView);
174 OnCollectionChanged (new CollectionChangeEventArgs (
175 CollectionChangeAction.Add, dataGridViewCell));
178 void IList.Remove (object value)
180 Remove (value as DataGridViewCell);
183 public virtual void Remove (DataGridViewCell cell)
185 base.List.Remove (cell);
187 OnCollectionChanged (new CollectionChangeEventArgs (
188 CollectionChangeAction.Remove, cell));
191 public virtual void RemoveAt (int index)
193 DataGridViewCell cell = this [index];
194 base.List.RemoveAt (index);
196 OnCollectionChanged (new CollectionChangeEventArgs (
197 CollectionChangeAction.Remove, cell));
200 private void ReIndex ()
202 for (int i = 0; i < base.List.Count; i++)
203 this[i].SetColumnIndex (i);
206 protected override ArrayList List {
207 get { return base.List; }
210 protected void OnCollectionChanged (CollectionChangeEventArgs e)
212 if (CollectionChanged != null) {
213 CollectionChanged(this, e);