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

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;

}

}
 
A

Alan Green

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top