The ItemCount vs TotalItemCount gotcha

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…

Advertisements

~ by Luis Abreu on April 13, 2010.

2 Responses to “The ItemCount vs TotalItemCount gotcha”

  1. 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. 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…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: