1 //------------------------------------------------------------------------------ 2 // <copyright file="ColumnCollection.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.Web.UI; 13 14 /// <devdoc> 15 /// <para>Represents the collection of columns to be displayed in 16 /// a <see cref='System.Web.UI.WebControls.DataGrid'/> 17 /// control.</para> 18 /// </devdoc> 19 public sealed class DataGridColumnCollection : ICollection, IStateManager { 20 21 private DataGrid owner; 22 private ArrayList columns; 23 private bool marked; 24 25 26 /// <devdoc> 27 /// <para>Initializes a new instance of <see cref='System.Web.UI.WebControls.DataGridColumnCollection'/> class.</para> 28 /// </devdoc> DataGridColumnCollection(DataGrid owner, ArrayList columns)29 public DataGridColumnCollection(DataGrid owner, ArrayList columns) { 30 this.owner = owner; 31 this.columns = columns; 32 } 33 34 35 /// <devdoc> 36 /// <para>Gets the number of columns in the collection. This property is read-only.</para> 37 /// </devdoc> 38 [ 39 Browsable(false) 40 ] 41 public int Count { 42 get { 43 return columns.Count; 44 } 45 } 46 47 48 /// <devdoc> 49 /// <para>Gets a value that specifies whether items in the <see cref='System.Web.UI.WebControls.DataGridColumnCollection'/> can be 50 /// modified. This property is read-only.</para> 51 /// </devdoc> 52 [ 53 Browsable(false) 54 ] 55 public bool IsReadOnly { 56 get { 57 return false; 58 } 59 } 60 61 62 /// <devdoc> 63 /// <para>Gets a value that indicates whether the <see cref='System.Web.UI.WebControls.DataGridColumnCollection'/> is thread-safe. This property is read-only.</para> 64 /// </devdoc> 65 [ 66 Browsable(false) 67 ] 68 public bool IsSynchronized { 69 get { 70 return false; 71 } 72 } 73 74 75 /// <devdoc> 76 /// <para>Gets the object used to synchronize access to the collection. This property is read-only. </para> 77 /// </devdoc> 78 [ 79 Browsable(false) 80 ] 81 public Object SyncRoot { 82 get { 83 return this; 84 } 85 } 86 87 88 /// <devdoc> 89 /// <para>Gets a <see cref='System.Web.UI.WebControls.DataGridColumn'/> at the specified index in the 90 /// collection.</para> 91 /// </devdoc> 92 [ 93 Browsable(false) 94 ] 95 public DataGridColumn this[int index] { 96 get { 97 return (DataGridColumn)columns[index]; 98 } 99 } 100 101 102 103 /// <devdoc> 104 /// <para>Appends a <see cref='System.Web.UI.WebControls.DataGridColumn'/> to the collection.</para> 105 /// </devdoc> Add(DataGridColumn column)106 public void Add(DataGridColumn column) { 107 AddAt(-1, column); 108 } 109 110 111 /// <devdoc> 112 /// <para>Inserts a <see cref='System.Web.UI.WebControls.DataGridColumn'/> to the collection 113 /// at the specified index.</para> 114 /// </devdoc> AddAt(int index, DataGridColumn column)115 public void AddAt(int index, DataGridColumn column) { 116 if (column == null) { 117 throw new ArgumentNullException("column"); 118 } 119 if (index == -1) { 120 columns.Add(column); 121 } 122 else { 123 columns.Insert(index, column); 124 } 125 column.SetOwner(owner); 126 if (marked) 127 ((IStateManager)column).TrackViewState(); 128 OnColumnsChanged(); 129 } 130 131 132 /// <devdoc> 133 /// <para>Empties the collection of all <see cref='System.Web.UI.WebControls.DataGridColumn'/> objects.</para> 134 /// </devdoc> Clear()135 public void Clear() { 136 columns.Clear(); 137 OnColumnsChanged(); 138 } 139 140 141 /// <devdoc> 142 /// <para>Copies the contents of the entire collection into an <see cref='System.Array' qualify='true'/> appending at 143 /// the specified index of the <see cref='System.Array' qualify='true'/>.</para> 144 /// </devdoc> CopyTo(Array array, int index)145 public void CopyTo(Array array, int index) { 146 if (array == null) { 147 throw new ArgumentNullException("array"); 148 } 149 for (IEnumerator e = this.GetEnumerator(); e.MoveNext();) 150 array.SetValue(e.Current, index++); 151 } 152 153 154 /// <devdoc> 155 /// <para>Creates an enumerator for the <see cref='System.Web.UI.WebControls.DataGridColumnCollection'/> used to iterate through the collection.</para> 156 /// </devdoc> GetEnumerator()157 public IEnumerator GetEnumerator() { 158 return columns.GetEnumerator(); 159 } 160 161 162 163 /// <devdoc> 164 /// <para>Returns the index of the first occurrence of a value in a <see cref='System.Web.UI.WebControls.DataGridColumn'/>.</para> 165 /// </devdoc> IndexOf(DataGridColumn column)166 public int IndexOf(DataGridColumn column) { 167 if (column != null) { 168 return columns.IndexOf(column); 169 } 170 return -1; 171 } 172 173 174 /// <devdoc> 175 /// </devdoc> OnColumnsChanged()176 private void OnColumnsChanged() { 177 if (owner != null) { 178 owner.OnColumnsChanged(); 179 } 180 } 181 182 183 /// <devdoc> 184 /// <para>Removes a <see cref='System.Web.UI.WebControls.DataGridColumn'/> from the collection at the specified 185 /// index.</para> 186 /// </devdoc> RemoveAt(int index)187 public void RemoveAt(int index) { 188 if ((index >= 0) && (index < Count)) { 189 columns.RemoveAt(index); 190 OnColumnsChanged(); 191 } 192 else { 193 throw new ArgumentOutOfRangeException("index"); 194 } 195 } 196 197 198 /// <devdoc> 199 /// <para>Removes the specified <see cref='System.Web.UI.WebControls.DataGridColumn'/> from the collection.</para> 200 /// </devdoc> Remove(DataGridColumn column)201 public void Remove(DataGridColumn column) { 202 int index = IndexOf(column); 203 if (index >= 0) { 204 RemoveAt(index); 205 } 206 } 207 208 209 210 /// <internalonly/> 211 /// <devdoc> 212 /// Return true if tracking state changes. 213 /// </devdoc> 214 bool IStateManager.IsTrackingViewState { 215 get { 216 return marked; 217 } 218 } 219 220 221 /// <internalonly/> 222 /// <devdoc> 223 /// Load previously saved state. 224 /// </devdoc> IStateManager.LoadViewState(object savedState)225 void IStateManager.LoadViewState(object savedState) { 226 if (savedState != null) { 227 object[] columnsState = (object[])savedState; 228 229 if (columnsState.Length == columns.Count) { 230 for (int i = 0; i < columnsState.Length; i++) { 231 if (columnsState[i] != null) { 232 ((IStateManager)columns[i]).LoadViewState(columnsState[i]); 233 } 234 } 235 } 236 } 237 } 238 239 240 /// <internalonly/> 241 /// <devdoc> 242 /// Start tracking state changes. 243 /// </devdoc> IStateManager.TrackViewState()244 void IStateManager.TrackViewState() { 245 marked = true; 246 247 int columnCount = columns.Count; 248 for (int i = 0; i < columnCount; i++) { 249 ((IStateManager)columns[i]).TrackViewState(); 250 } 251 } 252 253 254 /// <internalonly/> 255 /// <devdoc> 256 /// Return object containing state changes. 257 /// </devdoc> IStateManager.SaveViewState()258 object IStateManager.SaveViewState() { 259 int columnCount = columns.Count; 260 object[] columnsState = new object[columnCount]; 261 bool savedState = false; 262 263 for (int i = 0; i < columnCount; i++) { 264 columnsState[i] = ((IStateManager)columns[i]).SaveViewState(); 265 if (columnsState[i] != null) 266 savedState = true; 267 } 268 269 return savedState ? columnsState : null; 270 } 271 } 272 } 273