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>
26 using System.Collections;
27 using System.ComponentModel;
29 namespace System.Windows.Forms
31 [ListBindable (false)]
32 public class DataGridViewCellCollection : BaseCollection, IList, ICollection, IEnumerable
34 private DataGridViewRow dataGridViewRow;
36 public DataGridViewCellCollection (DataGridViewRow dataGridViewRow) : base()
38 this.dataGridViewRow = dataGridViewRow;
41 bool IList.IsFixedSize {
42 get { return base.List.IsFixedSize; }
45 object IList.this [int index] {
46 get { return this [index]; }
47 set { this [index] = value as DataGridViewCell; }
50 public DataGridViewCell this [int index] {
51 get { return (DataGridViewCell) base.List [index]; }
54 throw new ArgumentNullException ("value");
60 internal DataGridViewCell GetCellInternal (int colIndex)
62 return (DataGridViewCell) base.List [colIndex];
65 public DataGridViewCell this [string columnName] {
67 if (columnName == null)
68 throw new ArgumentNullException ("columnName");
70 foreach (DataGridViewCell cell in base.List) {
71 if (string.Compare (cell.OwningColumn.Name, columnName, true) == 0)
75 throw new ArgumentException (string.Format (
76 "Column name {0} cannot be found.",
77 columnName), "columnName");
80 if (columnName == null)
81 throw new ArgumentNullException ("columnName");
83 throw new ArgumentNullException ("value");
85 for (int i = 0; i < base.List.Count; i++) {
86 DataGridViewCell cell = (DataGridViewCell) base.List [i];
87 if (string.Compare (cell.OwningColumn.Name, columnName, true) == 0) {
96 internal DataGridViewCell GetBoundCell (string dataPropertyName)
98 foreach (DataGridViewCell cell in base.List) {
99 if (string.Compare (cell.OwningColumn.DataPropertyName, dataPropertyName, true) == 0)
106 public event CollectionChangeEventHandler CollectionChanged;
108 int IList.Add (object value)
110 return Add (value as DataGridViewCell);
113 public virtual int Add (DataGridViewCell dataGridViewCell)
115 int result = base.List.Add (dataGridViewCell);
116 dataGridViewCell.SetOwningRow (dataGridViewRow);
117 dataGridViewCell.SetColumnIndex (result);
118 dataGridViewCell.SetDataGridView (dataGridViewRow.DataGridView);
119 OnCollectionChanged (new CollectionChangeEventArgs (
120 CollectionChangeAction.Add, dataGridViewCell));
124 internal void Replace (int columnIndex, DataGridViewCell dataGridViewCell)
126 RemoveAt (columnIndex);
127 Insert (columnIndex, dataGridViewCell);
130 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
131 public virtual void AddRange (params DataGridViewCell[] dataGridViewCells)
133 foreach (DataGridViewCell cell in dataGridViewCells)
137 public virtual void Clear ()
142 bool IList.Contains (object value)
144 return Contains (value as DataGridViewCell);
147 public virtual bool Contains (DataGridViewCell dataGridViewCell)
149 return base.List.Contains (dataGridViewCell);
152 public void CopyTo (DataGridViewCell[] array, int index)
154 base.List.CopyTo (array, index);
157 int IList.IndexOf (object value)
159 return IndexOf (value as DataGridViewCell);
162 public int IndexOf (DataGridViewCell dataGridViewCell)
164 return base.List.IndexOf (dataGridViewCell);
167 void IList.Insert (int index, object value)
169 Insert (index, value as DataGridViewCell);
172 public virtual void Insert (int index, DataGridViewCell dataGridViewCell)
174 base.List.Insert (index, dataGridViewCell);
175 dataGridViewCell.SetOwningRow (dataGridViewRow);
176 dataGridViewCell.SetColumnIndex (index);
177 dataGridViewCell.SetDataGridView (dataGridViewRow.DataGridView);
179 OnCollectionChanged (new CollectionChangeEventArgs (
180 CollectionChangeAction.Add, dataGridViewCell));
183 void IList.Remove (object value)
185 Remove (value as DataGridViewCell);
188 public virtual void Remove (DataGridViewCell cell)
190 base.List.Remove (cell);
192 OnCollectionChanged (new CollectionChangeEventArgs (
193 CollectionChangeAction.Remove, cell));
196 public virtual void RemoveAt (int index)
198 DataGridViewCell cell = this [index];
199 base.List.RemoveAt (index);
201 OnCollectionChanged (new CollectionChangeEventArgs (
202 CollectionChangeAction.Remove, cell));
205 private void ReIndex ()
207 for (int i = 0; i < base.List.Count; i++)
208 this[i].SetColumnIndex (i);
211 protected override ArrayList List {
212 get { return base.List; }
215 protected void OnCollectionChanged (CollectionChangeEventArgs e)
217 if (CollectionChanged != null) {
218 CollectionChanged(this, e);