O
Oliver Hopton
Hello,
I'm trying to implement a PageableCollection class so I can easily add
custom paging functionality to my apps without the overhead of using the
DataGrid's paging functionality and the huge ViewState that generates. What
I have done works fine when I use foreach to iterate over my collection but
when I bind it to a DataGrid the DataGrid doesn't appear to use my
Enumerator, I'm guessing that it uses the base classes Enumerator or perhaps
it access the contents of the collection in a different way?
Anybody got any idea what is going on here and why?
Thanks,
Oliver.
Code follows:
public class PageableCollection : CollectionBase, IEnumerable
{
private bool fAllowPaging;
private int fPageSize;
private int fCurrentPage;
internal object this[int index]
{
get { return List[index]; }
set { List[index] = value; }
}
public int CurrentPage
{
get { return fCurrentPage; }
set { fCurrentPage = value; }
}
public bool AllowPaging
{
get { return fAllowPaging; }
set { fAllowPaging = value; }
}
public int PageSize
{
get { return fPageSize; }
set { fPageSize = value; }
}
public int PageCount
{
get
{
int retval = Count / fPageSize;
if (Count != fPageSize)
retval++;
return retval;
}
}
public PageableCollection()
{
fAllowPaging = false;
fPageSize = 10;
fCurrentPage = 1;
}
public new IEnumerator GetEnumerator()
{
return new PageableCollectionEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
internal class PageableCollectionEnumerator: IEnumerator
{
private int fIndex;
private PageableCollection CollectionToEnumerate;
public PageableCollectionEnumerator(PageableCollection
CollectionToEnumerate)
{
this.CollectionToEnumerate = CollectionToEnumerate;
Reset();
}
public void Reset()
{
if (CollectionToEnumerate.AllowPaging)
fIndex = (CollectionToEnumerate.PageSize *
CollectionToEnumerate.CurrentPage) - CollectionToEnumerate.PageSize;
else
fIndex = 0;
fIndex--;
}
public object Current
{
get
{
return CollectionToEnumerate[fIndex];
}
}
public bool MoveNext()
{
fIndex++;
if (fIndex >= CollectionToEnumerate.Count)
return false;
if (CollectionToEnumerate.AllowPaging)
//If paging is on check to see whether the index is less than the last index
for this page.
return (fIndex < (CollectionToEnumerate.PageSize *
CollectionToEnumerate.CurrentPage));
else
return true;
}
}
I'm trying to implement a PageableCollection class so I can easily add
custom paging functionality to my apps without the overhead of using the
DataGrid's paging functionality and the huge ViewState that generates. What
I have done works fine when I use foreach to iterate over my collection but
when I bind it to a DataGrid the DataGrid doesn't appear to use my
Enumerator, I'm guessing that it uses the base classes Enumerator or perhaps
it access the contents of the collection in a different way?
Anybody got any idea what is going on here and why?
Thanks,
Oliver.
Code follows:
public class PageableCollection : CollectionBase, IEnumerable
{
private bool fAllowPaging;
private int fPageSize;
private int fCurrentPage;
internal object this[int index]
{
get { return List[index]; }
set { List[index] = value; }
}
public int CurrentPage
{
get { return fCurrentPage; }
set { fCurrentPage = value; }
}
public bool AllowPaging
{
get { return fAllowPaging; }
set { fAllowPaging = value; }
}
public int PageSize
{
get { return fPageSize; }
set { fPageSize = value; }
}
public int PageCount
{
get
{
int retval = Count / fPageSize;
if (Count != fPageSize)
retval++;
return retval;
}
}
public PageableCollection()
{
fAllowPaging = false;
fPageSize = 10;
fCurrentPage = 1;
}
public new IEnumerator GetEnumerator()
{
return new PageableCollectionEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
internal class PageableCollectionEnumerator: IEnumerator
{
private int fIndex;
private PageableCollection CollectionToEnumerate;
public PageableCollectionEnumerator(PageableCollection
CollectionToEnumerate)
{
this.CollectionToEnumerate = CollectionToEnumerate;
Reset();
}
public void Reset()
{
if (CollectionToEnumerate.AllowPaging)
fIndex = (CollectionToEnumerate.PageSize *
CollectionToEnumerate.CurrentPage) - CollectionToEnumerate.PageSize;
else
fIndex = 0;
fIndex--;
}
public object Current
{
get
{
return CollectionToEnumerate[fIndex];
}
}
public bool MoveNext()
{
fIndex++;
if (fIndex >= CollectionToEnumerate.Count)
return false;
if (CollectionToEnumerate.AllowPaging)
//If paging is on check to see whether the index is less than the last index
for this page.
return (fIndex < (CollectionToEnumerate.PageSize *
CollectionToEnumerate.CurrentPage));
else
return true;
}
}