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>
29 using System
.ComponentModel
;
30 using System
.Collections
;
32 namespace System
.Windows
.Forms
{
34 public class DataGridViewRowCollection
: IList
, ICollection
, IEnumerable
{
36 private ArrayList list
;
37 private DataGridView dataGridView
;
39 private bool raiseEvent
= true;
41 public DataGridViewRowCollection (DataGridView dataGridView
) {
42 if (dataGridView
== null) {
43 throw new ArgumentException("DataGridView is null.");
45 this.dataGridView
= dataGridView
;
46 list
= new ArrayList();
50 get { return list.Count; }
53 public bool IsFixedSize
{
54 get { return list.IsFixedSize; }
57 public bool IsReadOnly
{
58 get { return list.IsReadOnly; }
61 public bool IsSynchronized
{
62 get { return list.IsSynchronized; }
65 object IList
.this [int index
] {
67 Console
.WriteLine("acceso");
70 set { list[index] = value as DataGridViewRow; }
73 public DataGridViewRow
this [int index
] {
75 // Accessing a System.Windows.Forms.DataGridViewRow with this indexer causes the row to become unshared. To keep the row shared, use the System.Windows.Forms.DataGridViewRowCollection.SharedRow method. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.
76 return (DataGridViewRow
) list
[index
];
80 public object SyncRoot
{
81 get { return list.SyncRoot; }
84 public event CollectionChangeEventHandler CollectionChange
;
86 public virtual int Add () {
87 return Add(dataGridView
.RowTemplate
.Clone() as DataGridViewRow
);
90 int IList
.Add(object o
) {
91 return Add(o
as DataGridViewRow
);
94 public virtual int Add (DataGridViewRow dataGridViewRow
) {
95 if (dataGridView
.DataSource
!= null) {
96 throw new InvalidOperationException("DataSource of DataGridView is not null.");
98 if (dataGridView
.Columns
.Count
== 0) {
99 throw new InvalidOperationException("DataGridView has no columns.");
101 dataGridViewRow
.SetIndex(list
.Count
);
102 dataGridViewRow
.SetDataGridView(dataGridView
);
103 int result
= list
.Add(dataGridViewRow
);
104 OnCollectionChange(new CollectionChangeEventArgs(CollectionChangeAction
.Add
, dataGridViewRow
));
106 DataGridView
.OnRowsAdded(new DataGridViewRowsAddedEventArgs(result
, 1));
111 public virtual int Add (int count
) {
113 throw new ArgumentOutOfRangeException("Count is less than or equeal to 0.");
115 if (dataGridView
.DataSource
!= null) {
116 throw new InvalidOperationException("DataSource of DataGridView is not null.");
118 if (dataGridView
.Columns
.Count
== 0) {
119 throw new InvalidOperationException("DataGridView has no columns.");
123 for (int i
= 0; i
< count
; i
++) {
124 result
= Add(dataGridView
.RowTemplate
.Clone() as DataGridViewRow
);
126 DataGridView
.OnRowsAdded(new DataGridViewRowsAddedEventArgs(result
- count
+ 1, count
));
131 public virtual int Add (params object[] values
) {
132 if (values
== null) {
133 throw new ArgumentNullException("values is null.");
135 if (dataGridView
.VirtualMode
) {
136 throw new InvalidOperationException("DataGridView is in virtual mode.");
138 DataGridViewRow row
= new DataGridViewRow();
139 int result
= Add(row
);
140 row
.SetValues(values
);
144 public virtual int AddCopies (int indexSource
, int count
) {
147 for (int i
= 0; i
< count
; i
++) {
148 lastIndex
= AddCopy(indexSource
);
150 DataGridView
.OnRowsAdded(new DataGridViewRowsAddedEventArgs(lastIndex
- count
+ 1, count
));
155 public virtual int AddCopy (int indexSource
) {
156 return Add((list
[indexSource
] as DataGridViewRow
).Clone() as DataGridViewRow
);
159 public virtual void AddRange (params DataGridViewRow
[] dataGridViewRows
) {
163 foreach (DataGridViewRow row
in dataGridViewRows
) {
164 lastIndex
= Add(row
);
167 DataGridView
.OnRowsAdded(new DataGridViewRowsAddedEventArgs(lastIndex
- count
+ 1, count
));
171 public virtual void Clear () {
175 bool IList
.Contains (object o
) {
176 return Contains(o
as DataGridViewRow
);
179 public virtual bool Contains (DataGridViewRow dataGridViewRow
) {
180 return list
.Contains(dataGridViewRow
);
183 public void CopyTo (Array array
, int index
) {
184 list
.CopyTo(array
, index
);
187 public void CopyTo (DataGridViewRow
[] array
, int index
) {
188 list
.CopyTo(array
, index
);
191 public IEnumerator
GetEnumerator () {
192 return list
.GetEnumerator();
195 public int GetFirstRow (DataGridViewElementStates includeFilter
) {
196 for (int i
= 0; i
< list
.Count
; i
++) {
197 DataGridViewRow row
= (DataGridViewRow
) list
[i
];
198 if ((row
.State
& includeFilter
) != 0) {
205 public int GetFirstRow (DataGridViewElementStates includeFilter
, DataGridViewElementStates excludeFilter
) {
206 for (int i
= 0; i
< list
.Count
; i
++) {
207 DataGridViewRow row
= (DataGridViewRow
) list
[i
];
208 if (((row
.State
& includeFilter
) != 0) && ((row
.State
& excludeFilter
) == 0)) {
215 public int GetLastRow (DataGridViewElementStates includeFilter
) {
216 for (int i
= list
.Count
- 1; i
>= 0; i
--) {
217 DataGridViewRow row
= (DataGridViewRow
) list
[i
];
218 if ((row
.State
& includeFilter
) != 0) {
225 public int GetNextRow (int indexStart
, DataGridViewElementStates includeFilter
) {
226 for (int i
= indexStart
+ 1; i
< list
.Count
; i
++) {
227 DataGridViewRow row
= (DataGridViewRow
) list
[i
];
228 if ((row
.State
& includeFilter
) != 0) {
235 public int GetNextRow (int indexStart
, DataGridViewElementStates includeFilter
, DataGridViewElementStates excludeFilter
) {
236 for (int i
= indexStart
+ 1; i
< list
.Count
; i
++) {
237 DataGridViewRow row
= (DataGridViewRow
) list
[i
];
238 if (((row
.State
& includeFilter
) != 0) && ((row
.State
& excludeFilter
) == 0)) {
245 public int GetPreviousRow (int indexStart
, DataGridViewElementStates includeFilter
) {
246 for (int i
= indexStart
- 1; i
>= 0; i
--) {
247 DataGridViewRow row
= (DataGridViewRow
) list
[i
];
248 if ((row
.State
& includeFilter
) != 0) {
255 public int GetPreviousRow (int indexStart
, DataGridViewElementStates includeFilter
, DataGridViewElementStates excludeFilter
) {
256 for (int i
= indexStart
- 1; i
>= 0; i
--) {
257 DataGridViewRow row
= (DataGridViewRow
) list
[i
];
258 if (((row
.State
& includeFilter
) != 0) && ((row
.State
& excludeFilter
) == 0)) {
265 public int GetRowCount (DataGridViewElementStates includeFilter
) {
267 foreach (DataGridViewRow row
in list
) {
268 if ((row
.State
& includeFilter
) != 0) {
275 public int GetRowsHeight (DataGridViewElementStates includeFilter
) {
277 foreach (DataGridViewRow row
in list
) {
278 if ((row
.State
& includeFilter
) != 0) {
279 result
+= row
.Height
;
285 public virtual DataGridViewElementStates
GetRowState (int rowIndex
) {
286 return (list
[rowIndex
] as DataGridViewRow
).State
;
289 int IList
.IndexOf (object o
) {
290 return IndexOf(o
as DataGridViewRow
);
293 public int IndexOf (DataGridViewRow dataGridViewRow
) {
294 return list
.IndexOf(dataGridViewRow
);
297 void IList
.Insert (int rowIndex
, object o
) {
298 Insert(rowIndex
, o
as DataGridViewRow
);
301 public virtual void Insert (int rowIndex
, DataGridViewRow dataGridViewRow
) {
302 dataGridViewRow
.SetIndex(rowIndex
);
303 dataGridViewRow
.SetDataGridView(dataGridView
);
304 list
[rowIndex
] = dataGridViewRow
;
305 OnCollectionChange(new CollectionChangeEventArgs(CollectionChangeAction
.Add
, dataGridViewRow
));
307 DataGridView
.OnRowsAdded(new DataGridViewRowsAddedEventArgs(rowIndex
, 1));
311 public virtual void Insert (int rowIndex
, int count
) {
312 int index
= rowIndex
;
314 for (int i
= 0; i
< count
; i
++) {
315 Insert(index
++, dataGridView
.RowTemplate
.Clone());
317 DataGridView
.OnRowsAdded(new DataGridViewRowsAddedEventArgs(rowIndex
, count
));
321 public virtual void Insert (int rowIndex
, params object[] values
) {
322 if (values
== null) {
323 throw new ArgumentNullException("Values is null.");
325 if (dataGridView
.VirtualMode
|| dataGridView
.DataSource
!= null) {
326 throw new InvalidOperationException();
328 DataGridViewRow row
= new DataGridViewRow();
329 row
.SetValues(values
);
330 Insert(rowIndex
, row
);
333 public virtual void InsertCopies (int indexSource
, int indexDestination
, int count
) {
335 int index
= indexDestination
;
336 for (int i
= 0; i
< count
; i
++) {
337 InsertCopy(indexSource
, index
++);
339 DataGridView
.OnRowsAdded(new DataGridViewRowsAddedEventArgs(indexDestination
, count
));
343 public virtual void InsertCopy (int indexSource
, int indexDestination
) {
344 Insert(indexDestination
, (list
[indexSource
] as DataGridViewRow
).Clone());
347 public virtual void InsertRange (int rowIndex
, params DataGridViewRow
[] dataGridViewRows
) {
349 int index
= rowIndex
;
351 foreach (DataGridViewRow row
in dataGridViewRows
) {
352 Insert(index
++, row
);
355 DataGridView
.OnRowsAdded(new DataGridViewRowsAddedEventArgs(rowIndex
, count
));
359 void IList
.Remove (object o
) {
360 Remove(o
as DataGridViewRow
);
363 public virtual void Remove (DataGridViewRow dataGridViewRow
) {
364 list
.Remove(dataGridViewRow
);
365 OnCollectionChange(new CollectionChangeEventArgs(CollectionChangeAction
.Remove
, dataGridViewRow
));
366 DataGridView
.OnRowsRemoved(new DataGridViewRowsRemovedEventArgs(dataGridViewRow
.Index
, 1));
369 public virtual void RemoveAt (int index
) {
370 DataGridViewRow row
= this[index
];
371 list
.RemoveAt(index
);
372 OnCollectionChange(new CollectionChangeEventArgs(CollectionChangeAction
.Remove
, row
));
373 DataGridView
.OnRowsRemoved(new DataGridViewRowsRemovedEventArgs(index
, 1));
376 public DataGridViewRow
SharedRow (int rowIndex
) {
377 return (DataGridViewRow
) list
[rowIndex
];
380 protected DataGridView DataGridView
{
381 get { return dataGridView; }
384 protected ArrayList List
{
388 protected virtual void OnCollectionChange (CollectionChangeEventArgs e
) {
389 if (CollectionChange
!= null) {
390 CollectionChange(this, e
);
394 /************************/
396 internal void InternalAdd (DataGridViewRow dataGridViewRow
) {
397 dataGridViewRow
.SetIndex(list
.Count
);
398 dataGridViewRow
.SetDataGridView(dataGridView
);
399 list
.Add(dataGridViewRow
);
402 internal ArrayList RowIndexSortedArrayList
{
404 ArrayList result
= (ArrayList
) list
.Clone();
405 result
.Sort(new RowIndexComparator());
410 private class RowIndexComparator
: IComparer
{
412 public int Compare (object o1
, object o2
) {
413 DataGridViewRow row1
= (DataGridViewRow
) o1
;
414 DataGridViewRow row2
= (DataGridViewRow
) o2
;
415 if (row1
.Index
< row2
.Index
) {
418 else if (row1
.Index
> row2
.Index
) {
428 /************************/