Collection class bound to DataGrid doesn't use my custom enumerator!

Collection class bound to DataGrid doesn't use my custom enumerator!

Post by Oliver Hop » Thu, 02 Oct 2003 13:14:46


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;

}

}
 
 
 

Collection class bound to DataGrid doesn't use my custom enumerator!

Post by Alan Gree » Fri, 31 Oct 2003 07:22:47

I've tried this in VB, and the Windows DataGrid appears to merely count
its way up the list rather than use the enumerator. Therefore it can be
tracked using the collection Item property, and could possibly be paged
using the Count and Item properties. However the Web DataGrid doesn't
seem to touch any of my collection class code.

Can anyone shed any light on this?



*** Sent via Developersdex http://www.yqcomputer.com/ ***
Don't just participate in USENET...get rewarded for it!