2 // System.Web.UI.WebControls.ListView
5 // Marek Habersack (mhabersack@novell.com)
7 // (C) 2007-2008 Novell, Inc
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.ComponentModel;
35 namespace System.Web.UI.WebControls
37 public class ListViewPagedDataSource : ICollection, IEnumerable, ITypedList
39 sealed class ListEnumerator : IEnumerator
46 public object Current {
47 get { return list [startIndex + index]; }
50 public ListEnumerator (IList list, int startIndex, int end)
54 this.startIndex = startIndex;
55 this.end = startIndex + end;
58 public bool MoveNext ()
61 return (startIndex + index) < end;
65 // http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewpageddatasource.getenumerator.aspx
73 sealed class CollectionEnumerator : IEnumerator
78 ICollection collection;
79 IEnumerator enumerator;
81 public object Current {
83 if (enumerator != null)
84 return enumerator.Current;
90 public CollectionEnumerator (ICollection collection, int startIndex, int end)
92 this.collection = collection;
94 this.startIndex = startIndex;
98 public bool MoveNext ()
100 if (enumerator == null) {
101 enumerator = collection.GetEnumerator ();
102 for (int i = 0; i < startIndex; i++)
103 enumerator.MoveNext ();
107 enumerator.MoveNext ();
108 return (startIndex + index) < end;
112 // http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewpageddatasource.getenumerator.aspx
121 public ListViewPagedDataSource ()
125 AllowServerPaging = false;
129 public void CopyTo (Array array, int index)
133 public IEnumerator GetEnumerator ()
135 IEnumerable ds = DataSource;
140 IList list = ds as IList;
142 return new ListEnumerator (list, AllowServerPaging ? 0 : StartRowIndex, Count);
144 ICollection collection = ds as ICollection;
145 if (collection != null)
146 return new CollectionEnumerator (collection, AllowServerPaging ? 0 : StartRowIndex, Count);
148 return ds.GetEnumerator ();
151 public PropertyDescriptorCollection GetItemProperties (PropertyDescriptor [] listAccessors)
153 IEnumerable ds = DataSource;
155 if (ds == null || !(ds is ITypedList))
158 return ((ITypedList) ds).GetItemProperties (listAccessors);
161 public string GetListName (PropertyDescriptor [] listAccessors)
166 public bool AllowServerPaging {
173 IEnumerable ds = DataSource;
177 bool onLastPage = OnLastPage;
178 int maxRows = MaximumRows;
179 if (!onLastPage && maxRows >= 0)
182 // LAMESPEC: MSDN says that DataSourceCount should be subtracted
183 // from StartRowIndex
184 return DataSourceCount - StartRowIndex;
188 public IEnumerable DataSource {
193 public int DataSourceCount {
195 IEnumerable ds = DataSource;
199 if (!(ds is ICollection))
200 throw new InvalidOperationException ("The data source object does not implement the System.Collections..::.ICollection interface.");
202 if (IsServerPagingEnabled)
203 return TotalRowCount;
205 return ((ICollection) ds).Count;
209 public bool IsReadOnly {
210 get { return false; }
213 public bool IsServerPagingEnabled {
214 get { return AllowServerPaging; }
217 public bool IsSynchronized {
218 get { return false; }
221 public int MaximumRows {
226 public int StartRowIndex {
231 public object SyncRoot {
235 public int TotalRowCount {
241 get { return ((StartRowIndex + MaximumRows) >= DataSourceCount); }