3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Namespace: System.Web.UI.WebControls
24 * Class: PagedDataSource
26 * Author: Gaurav Vaish
27 * Maintainer: gvaish@iitk.ac.in
28 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
32 * (C) Gaurav Vaish (2002)
36 using System.ComponentModel;
37 using System.Collections;
41 namespace System.Web.UI.WebControls
43 public sealed class PagedDataSource : ICollection, IEnumerable, ITypedList
46 private bool allowPaging;
47 private int currentPageIndex;
48 private bool allowCustomPaging;
49 private int virtualCount;
51 private IEnumerable dataSource;
53 public PagedDataSource()
58 private void Initialize()
63 allowCustomPaging = false;
67 public bool AllowCustomPaging
71 return allowCustomPaging;
75 allowCustomPaging = value;
79 public bool AllowPaging
95 if(dataSource != null)
99 return DataSourceCount;
101 if(IsCustomPagingEnabled)
107 return (DataSourceCount - FirstIndexInPage);
115 public int CurrentPageIndex
119 return currentPageIndex;
124 currentPageIndex = value;
128 public IEnumerable DataSource
140 public int DataSourceCount
144 if(dataSource != null)
146 if(IsCustomPagingEnabled)
150 if(dataSource is ICollection)
152 return ((ICollection)dataSource).Count;
154 throw new HttpException(HttpRuntime.FormatResourceString("PagedDataSource_Cannot_Get_Count"));
160 public int FirstIndexInPage
164 if(dataSource != null && IsPagingEnabled && !IsCustomPagingEnabled)
166 return (currentPageIndex * pageSize);
172 public bool IsCustomPagingEnabled
176 return (IsPagingEnabled && allowCustomPaging);
180 public bool IsFirstPage
184 return (!IsPagingEnabled || (CurrentPageIndex == 0));
188 public bool IsLastPage
192 return (!IsPagingEnabled || (CurrentPageIndex == (PageCount - 1)));
196 public bool IsPagingEnabled
200 return (allowPaging && pageSize != 0);
204 public bool IsReadOnly
212 public bool IsSynchronized
224 if(dataSource != null) {
228 int total = DataSourceCount;
229 return (total + pageSize - 1)/pageSize;
247 public object SyncRoot
255 public int VirtualCount
263 virtualCount = value;
267 public void CopyTo(Array array, int index)
269 IEnumerator enumerator = this.GetEnumerator();
270 if(enumerator == null) return;
272 while(enumerator.MoveNext())
273 array.SetValue(enumerator.Current, index++);
276 public IEnumerator GetEnumerator()
278 int fInd = FirstIndexInPage;
280 if(dataSource is ICollection)
285 if(dataSource is IList)
287 return (new PrivateListEnumerator((IList)dataSource, fInd, count));
289 if(dataSource is Array)
291 return (new PrivateArrayEnumerator((object[])dataSource, fInd, count));
293 if(dataSource is ICollection)
295 return (new PrivateICollectionEnumerator((ICollection)dataSource, fInd, count));
297 if(allowCustomPaging)
299 return (new PrivateIEnumeratorEnumerator(dataSource.GetEnumerator(), Count));
301 return dataSource.GetEnumerator();
304 class PrivateIEnumeratorEnumerator : IEnumerator
309 private IEnumerator enumerator;
311 public PrivateIEnumeratorEnumerator(IEnumerator enumerator, int count)
313 this.enumerator = enumerator;
318 public bool MoveNext()
320 enumerator.MoveNext();
322 return (index < max);
331 public object Current
335 return enumerator.Current;
340 class PrivateICollectionEnumerator : IEnumerator
346 private ICollection collection;
347 private IEnumerator collEnum;
349 public PrivateICollectionEnumerator(ICollection collection, int start, int count)
351 this.collection = collection;
355 if(max > collection.Count)
357 max = collection.Count;
361 public bool MoveNext()
366 collEnum = collection.GetEnumerator();
367 while(cIndex < start)
375 return (start + index < max);
384 public object Current
388 return collEnum.Current;
393 class PrivateArrayEnumerator : IEnumerator
398 private object[] values;
400 public PrivateArrayEnumerator(object[] values, int start, int count)
402 this.values = values;
406 if(max > this.values.Length)
408 max = this.values.Length;
412 public bool MoveNext()
415 return (index + start < max);
423 public object Current
429 return values[index + start];
431 throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
436 class PrivateListEnumerator : IEnumerator
441 private IList collection;
443 public PrivateListEnumerator(IList list, int start, int count)
455 public bool MoveNext()
458 return (index + start < max);
466 public object Current
472 return collection[index + start];
474 throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
479 public string GetListName(PropertyDescriptor[] listAccessors)
484 public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
486 if(dataSource != null)
488 if(dataSource is ITypedList)
490 return ((ITypedList)dataSource).GetItemProperties(listAccessors);