Apr 13

If you’ve been following this blog, then you know that I’ve been updating an existing project to .NET 4.0 (btw, don’t forget to download VS 2010; it’s already available for MSDN subscribers). This upgrade has been a great way to start using several great new features in a real world project. One extra benefit has been using Silverlight (4.0 RC) for building the UIs of the apps.

Since I’ve finally been able to use Silverlight in a “real world project”, I’ve went ahead and tried several things (including the so called view model pattern – MVVM pattern, if you really must be picky :),,). Now, one of the things I needed to do was add paging to a DataGrid control. If you’ve played with it, you’ll know that everything works just fine for auto-paging (ie, when you pass everything and set the page size to something smaller than the total number of items in the collection).

Unfortunately, this is something which isn’t really useful, right? In the real world, I do need to page data but I’m only returning a limited number of items from my server. After reading the docs and following some very good posts from Brad Abrams, I’ve noticed that I could do what I wanted by making my collection implement the IPagedCollectionView interface. As always, I’ve went with a hunch and implement the interface in a “logical” way. Now, the problem was that my pager was always returning page X of 1. WTF? 1? Why? Simple: ItemCount is supposed to return the *total* number of elements *before* pagination. Out of curiosity, I’ve went ahead and read the docs for TotalItemCount (notice that their definitions in the interface page are really similar). And that’s where I’ve found the difference: TotalItemCount is supposed to return –1 when the total number of items is unknown (btw, the DataPager does have a IsTotalItemCountFixed property which you can also set to configure the behavior of the pager when it reaches the last page of data).

Bottom line: whenever you’re implementing the IPagedCollectionView interface for “custom” paging, don’t forget that ItemCount is supposed to have the number of items before paging (and not the number of items maintained in the current collection). Lesson learnt…

2 comments so far

  1. razor24
    5:08 am - 6-9-2010

    So if I have 100 items on the server and my client side page size is 10 and I”m paging thru my collection should ItemCount and TotalItemCount be set to 100 with IsTotalItemCountFixed set to true? This is what I currently do and it works. It just seems these two properties should always be the same or should ItemCount be 10 and TotalItemCount be 100?

  2. luisabreu
    8:08 am - 6-9-2010

    Yes, the way to go is to make itemcount=totalitemcount if you know the total number of elements. Not sure on why it works this way, but that”s the way it is…