1 //------------------------------------------------------------------------------
2 // <copyright file="TableRowCollection.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.UI.WebControls {
8 
9     using System;
10     using System.Collections;
11     using System.ComponentModel;
12     using System.Drawing.Design;
13     using System.Web;
14     using System.Web.UI;
15 
16     /// <devdoc>
17     /// <para>Encapsulates the collection of <see cref='System.Web.UI.WebControls.TableRow'/> objects within a <see cref='System.Web.UI.WebControls.Table'/> control.</para>
18     /// </devdoc>
19     [
20     Editor("System.Web.UI.Design.WebControls.TableRowsCollectionEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor))
21     ]
22     public sealed class TableRowCollection : IList {
23 
24 
25         /// <devdoc>
26         ///    A protected field of type <see cref='System.Web.UI.WebControls.Table'/>. Represents the <see cref='System.Web.UI.WebControls.TableRow'/> collection internally.
27         /// </devdoc>
28         private Table owner;
29 
30 
31         /// <devdoc>
32         /// </devdoc>
TableRowCollection(Table owner)33         internal TableRowCollection(Table owner) {
34             this.owner = owner;
35         }
36 
37 
38         /// <devdoc>
39         ///    Gets the
40         ///    count of <see cref='System.Web.UI.WebControls.TableRow'/> in the collection.
41         /// </devdoc>
42         public int Count {
43             get {
44                 if (owner.HasControls()) {
45                     return owner.Controls.Count;
46                 }
47                 return 0;
48             }
49         }
50 
51 
52         /// <devdoc>
53         ///    <para>
54         ///       Gets a <see cref='System.Web.UI.WebControls.TableRow'/> referenced by the
55         ///       specified ordinal index value.
56         ///    </para>
57         /// </devdoc>
58         public TableRow this[int index] {
59             get {
60                 return(TableRow)owner.Controls[index];
61             }
62         }
63 
64 
65 
66         /// <devdoc>
67         ///    <para>
68         ///       Adds the specified <see cref='System.Web.UI.WebControls.TableRow'/> to the end of the collection.
69         ///    </para>
70         /// </devdoc>
Add(TableRow row)71         public int Add(TableRow row) {
72             AddAt(-1, row);
73             return owner.Controls.Count - 1;
74         }
75 
76 
77         /// <devdoc>
78         ///    <para>
79         ///       Adds the specified <see cref='System.Web.UI.WebControls.TableRow'/> to the collection at the specified
80         ///       index location.
81         ///    </para>
82         /// </devdoc>
AddAt(int index, TableRow row)83         public void AddAt(int index, TableRow row) {
84             owner.Controls.AddAt(index, row);
85             if (row.TableSection != TableRowSection.TableBody) {
86                 owner.HasRowSections = true;
87             }
88         }
89 
90 
91         /// <devdoc>
92         /// </devdoc>
AddRange(TableRow[] rows)93         public void AddRange(TableRow[] rows) {
94             if (rows == null) {
95                 throw new ArgumentNullException("rows");
96             }
97             foreach(TableRow row in rows) {
98                 Add(row);
99             }
100         }
101 
102 
103         /// <devdoc>
104         /// <para>Removes all <see cref='System.Web.UI.WebControls.TableRow'/> controls from the collection.</para>
105         /// </devdoc>
Clear()106         public void Clear() {
107             if (owner.HasControls()) {
108                 owner.Controls.Clear();
109                 owner.HasRowSections = false;
110             }
111         }
112 
113 
114         /// <devdoc>
115         ///    <para> Returns an ordinal index value that denotes the position of the specified
116         ///    <see cref='System.Web.UI.WebControls.TableRow'/> within the collection. </para>
117         /// </devdoc>
GetRowIndex(TableRow row)118         public int GetRowIndex(TableRow row) {
119             if (owner.HasControls()) {
120                 return owner.Controls.IndexOf(row);
121             }
122             return -1;
123         }
124 
125 
126         /// <devdoc>
127         ///    <para>
128         ///       Returns an enumerator of all <see cref='System.Web.UI.WebControls.TableRow'/> controls within the
129         ///       collection.
130         ///    </para>
131         /// </devdoc>
GetEnumerator()132         public IEnumerator GetEnumerator() {
133             return owner.Controls.GetEnumerator();
134         }
135 
136 
137         /// <devdoc>
138         /// <para>Copies contents from the collection to the specified <see cref='System.Array' qualify='true'/> with the
139         ///    specified starting index.</para>
140         /// </devdoc>
CopyTo(Array array, int index)141         public void CopyTo(Array array, int index) {
142             for (IEnumerator e = this.GetEnumerator(); e.MoveNext();)
143                 array.SetValue(e.Current, index++);
144         }
145 
146 
147         /// <devdoc>
148         ///    <para>
149         ///       Gets the object that can be used to synchronize access to the collection. In
150         ///       this case, it is the collection itself.
151         ///    </para>
152         /// </devdoc>
153         public Object SyncRoot {
154             get { return this;}
155         }
156 
157 
158         /// <devdoc>
159         ///    <para>
160         ///       Gets a value indicating whether the collection is read-only.
161         ///    </para>
162         /// </devdoc>
163         public bool IsReadOnly {
164             get { return false;}
165         }
166 
167 
168         /// <devdoc>
169         ///    <para>
170         ///       Gets a value indicating whether access to the collection is synchronized
171         ///       (thread-safe).
172         ///    </para>
173         /// </devdoc>
174         public bool IsSynchronized {
175             get { return false;}
176         }
177 
178 
179         /// <devdoc>
180         /// <para>Removes the specified <see cref='System.Web.UI.WebControls.TableRow'/> from the collection.</para>
181         /// </devdoc>
Remove(TableRow row)182         public void Remove(TableRow row) {
183             owner.Controls.Remove(row);
184         }
185 
186 
187         /// <devdoc>
188         /// <para>Removes the <see cref='System.Web.UI.WebControls.TableRow'/> from the collection at the specified
189         ///    index location.</para>
190         /// </devdoc>
RemoveAt(int index)191         public void RemoveAt(int index) {
192             owner.Controls.RemoveAt(index);
193         }
194 
195         // IList implementation, required by collection editor
196 
197         /// <internalonly/>
198         object IList.this[int index] {
199             get {
200                 return owner.Controls[index];
201             }
202             set {
203                 RemoveAt(index);
204                 AddAt(index, (TableRow)value);
205             }
206         }
207 
208 
209         /// <internalonly/>
210         bool IList.IsFixedSize {
211             get {
212                 return false;
213             }
214         }
215 
216 
217         /// <internalonly/>
IList.Add(object o)218         int IList.Add(object o) {
219             return Add((TableRow) o);
220         }
221 
222 
223         /// <internalonly/>
IList.Contains(object o)224         bool IList.Contains(object o) {
225             return owner.Controls.Contains((TableRow)o);
226         }
227 
228 
229         /// <internalonly/>
IList.IndexOf(object o)230         int IList.IndexOf(object o) {
231             return owner.Controls.IndexOf((TableRow)o);
232         }
233 
234 
235         /// <internalonly/>
IList.Insert(int index, object o)236         void IList.Insert(int index, object o) {
237             AddAt(index, (TableRow)o);
238         }
239 
240 
241         /// <internalonly/>
IList.Remove(object o)242         void IList.Remove(object o) {
243             Remove((TableRow)o);
244         }
245 
246     }
247 }
248 
249