2 * Namespace: System.Web.UI.WebControls
3 * Class: PagedDataSource
6 * Maintainer: gvaish@iitk.ac.in
7 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
11 * (C) Gaurav Vaish (2002)
15 using System.ComponentModel;
16 using System.Collections;
20 namespace System.Web.UI.WebControls
22 public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
25 private bool allowPaging;
26 private int currentPageIndex;
27 private bool allowCustomPaging;
28 private int virtualCount;
30 private IEnumerable dataSource;
32 public PagedDataSource()
37 private void Initialize()
42 allowCustomPaging = false;
46 public bool AllowCustomPaging
50 return allowCustomPaging;
54 allowCustomPaging = value;
58 public bool AllowPaging
74 if(dataSource != null)
78 return DataSourceCount;
80 if(IsCustomPagingEnabled)
86 return (DataSourceCount - FirstIndexInPage);
94 public int CurrentPageIndex
98 return currentPageIndex;
103 currentPageIndex = value;
107 public IEnumerable DataSource
119 public int DataSourceCount
123 if(dataSource != null)
125 if(IsCustomPagingEnabled)
129 if(dataSource is ICollection)
131 return ((ICollection)dataSource).Count;
133 throw new HttpException(HttpRuntime.FormatResourceString("PagedDataSource_Cannot_Get_Count"));
139 public int FirstIndexInPage
143 if(dataSource != null && IsPagingEnabled && !IsCustomPagingEnabled)
145 return (currentPageIndex * pageSize);
151 public bool IsCustomPagingEnabled
155 return (IsPagingEnabled && allowCustomPaging);
159 public bool IsFirstPage
163 return (!IsPagingEnabled || (CurrentPageIndex == 0));
167 public bool IsLastPage
171 return (!IsPagingEnabled || (CurrentPageIndex == PageCount));
175 public bool IsPagingEnabled
179 return (allowPaging && pageSize != 0);
183 public bool IsReadOnly
191 public bool IsSynchronized
203 if(dataSource != null)
205 int total = DataSourceCount;
210 return (total + pageSize - 1)/pageSize;
228 public object SyncRoot
236 public int VirtualCount
244 virtualCount = value;
248 public void CopyTo(Array array, int index)
250 foreach(object current in this)
252 array.SetValue(array, index++);
256 public IEnumerator GetEnumerator()
258 int fInd = FirstIndexInPage;
260 if(dataSource is ICollection)
265 if(dataSource is IList)
267 return (new PrivateListEnumerator((IList)dataSource, fInd, count));
269 if(dataSource is Array)
271 return (new PrivateArrayEnumerator((object[])dataSource, fInd, count));
273 if(dataSource is ICollection)
275 return (new PrivateICollectionEnumerator((ICollection)dataSource, fInd, count));
277 if(allowCustomPaging)
279 return (new PrivateIEnumeratorEnumerator(dataSource.GetEnumerator(), Count));
281 return dataSource.GetEnumerator();
284 class PrivateIEnumeratorEnumerator : IEnumerator
289 private IEnumerator enumerator;
291 public PrivateIEnumeratorEnumerator(IEnumerator enumerator, int count)
293 this.enumerator = enumerator;
298 public bool MoveNext()
300 enumerator.MoveNext();
302 return (index < max);
311 public object Current
315 return enumerator.Current;
320 class PrivateICollectionEnumerator : IEnumerator
326 private ICollection collection;
327 private IEnumerator collEnum;
329 public PrivateICollectionEnumerator(ICollection collection, int start, int count)
331 this.collection = collection;
335 if(max > collection.Count)
337 max = collection.Count;
341 public bool MoveNext()
346 collEnum = collection.GetEnumerator();
347 while(cIndex < start)
354 return (start + index < max);
363 public object Current
367 return collEnum.Current;
372 class PrivateArrayEnumerator : IEnumerator
377 private object[] values;
379 public PrivateArrayEnumerator(object[] values, int start, int count)
381 this.values = values;
385 if(max > this.values.Length)
387 max = this.values.Length;
391 public bool MoveNext()
394 return (index + start < max);
402 public object Current
408 return values[index + start];
410 throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
415 class PrivateListEnumerator : IEnumerator
420 private IList collection;
422 public PrivateListEnumerator(IList list, int start, int count)
434 public bool MoveNext()
437 return (index + start < max);
445 public object Current
451 return collection[index + start];
453 throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
458 public string GetListName(PropertyDescriptor[] listAccessors)
463 public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
465 if(dataSource != null)
467 if(dataSource is ITypedList)
469 return ((ITypedList)dataSource).GetItemProperties(listAccessors);