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;
52 DataSourceSelectArguments arguments;
57 private IEnumerable dataSource;
59 public PagedDataSource()
64 private void Initialize()
69 allowCustomPaging = false;
74 public DataSourceSelectArguments DataSourceSelectArguments {
75 get { return arguments; }
76 set { arguments = value; }
79 public DataSourceView DataSourceView {
84 public bool AllowServerPaging {
85 get { return serverPaging; }
86 set { serverPaging = value; }
89 public bool IsServerPagingEnabled {
90 get { return allowPaging && serverPaging; }
93 public void SetItemCountFromPageIndex (int highestPageIndex)
95 arguments.StartRowIndex = CurrentPageIndex * PageSize;
96 arguments.MaximumRows = (highestPageIndex - CurrentPageIndex) * PageSize + 1;
97 IEnumerable data = view.ExecuteSelect (arguments);
99 virtualCount = CurrentPageIndex * PageSize;
100 if (data is ICollection) {
101 virtualCount += ((ICollection)data).Count;
103 IEnumerator e = data.GetEnumerator ();
111 public bool AllowCustomPaging
115 return allowCustomPaging;
119 allowCustomPaging = value;
123 public bool AllowPaging
139 if(dataSource != null)
143 return DataSourceCount;
145 if(IsCustomPagingEnabled)
152 int n = DataSourceCount;
153 if (n == 0) return 0;
154 else return (n - FirstIndexInPage);
162 public int CurrentPageIndex
166 return currentPageIndex;
171 currentPageIndex = value;
175 public IEnumerable DataSource
187 public int DataSourceCount
191 if(dataSource != null)
199 if(IsCustomPagingEnabled)
203 if(dataSource is ICollection)
205 return ((ICollection)dataSource).Count;
207 throw new HttpException(HttpRuntime.FormatResourceString("PagedDataSource_Cannot_Get_Count"));
213 public int FirstIndexInPage
217 if(dataSource != null && IsPagingEnabled && !IsCustomPagingEnabled)
219 return (currentPageIndex * pageSize);
225 public bool IsCustomPagingEnabled
229 return (IsPagingEnabled && allowCustomPaging);
233 public bool IsFirstPage
237 return (!IsPagingEnabled || (CurrentPageIndex == 0));
241 public bool IsLastPage
245 return (!IsPagingEnabled || (CurrentPageIndex == (PageCount - 1)) || PageCount <= 1);
249 public bool IsPagingEnabled
253 return (allowPaging && pageSize != 0);
257 public bool IsReadOnly
265 public bool IsSynchronized
277 if(dataSource != null) {
281 int total = (DataSourceCount + pageSize - 1) / pageSize;
282 return (total > 0 ? total : 1);
300 public object SyncRoot
308 public int VirtualCount
316 virtualCount = value;
320 public void CopyTo(Array array, int index)
322 IEnumerator enumerator = this.GetEnumerator();
323 if(enumerator == null) return;
325 while(enumerator.MoveNext())
326 array.SetValue(enumerator.Current, index++);
329 public IEnumerator GetEnumerator()
332 int fInd = serverPaging ? 0 : FirstIndexInPage;
334 int fInd = FirstIndexInPage;
338 if(dataSource is ICollection)
343 if(dataSource is IList)
345 return (new PrivateListEnumerator((IList)dataSource, fInd, count));
347 if(dataSource is Array)
349 return (new PrivateArrayEnumerator((object[])dataSource, fInd, count));
351 if(dataSource is ICollection)
353 return (new PrivateICollectionEnumerator((ICollection)dataSource, fInd, count));
355 if(allowCustomPaging)
357 return (new PrivateIEnumeratorEnumerator(dataSource.GetEnumerator(), Count));
359 return dataSource.GetEnumerator();
362 class PrivateIEnumeratorEnumerator : IEnumerator
367 private IEnumerator enumerator;
369 public PrivateIEnumeratorEnumerator(IEnumerator enumerator, int count)
371 this.enumerator = enumerator;
376 public bool MoveNext()
378 enumerator.MoveNext();
380 return (index < max);
389 public object Current
393 return enumerator.Current;
398 class PrivateICollectionEnumerator : IEnumerator
404 private ICollection collection;
405 private IEnumerator collEnum;
407 public PrivateICollectionEnumerator(ICollection collection, int start, int count)
409 this.collection = collection;
413 if(max > collection.Count)
415 max = collection.Count;
419 public bool MoveNext()
424 collEnum = collection.GetEnumerator();
425 while(cIndex < start)
433 return (start + index < max);
442 public object Current
446 return collEnum.Current;
451 class PrivateArrayEnumerator : IEnumerator
456 private object[] values;
458 public PrivateArrayEnumerator(object[] values, int start, int count)
460 this.values = values;
464 if(max > this.values.Length)
466 max = this.values.Length;
470 public bool MoveNext()
473 return (index + start < max);
481 public object Current
487 return values[index + start];
489 throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
494 class PrivateListEnumerator : IEnumerator
499 private IList collection;
501 public PrivateListEnumerator(IList list, int start, int count)
513 public bool MoveNext()
516 return (index + start < max);
524 public object Current
530 return collection[index + start];
532 throw new InvalidOperationException("Enumerator_MoveNext_Not_Called");
537 public string GetListName(PropertyDescriptor[] listAccessors)
542 public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
544 if(dataSource != null)
546 if(dataSource is ITypedList)
548 return ((ITypedList)dataSource).GetItemProperties(listAccessors);