Mark Gilbert's Blog

Science and technology, served light and fluffy.

Can you please page Mark for me? ASP.NET DataGrid and Custom Paging

One of the main reasons I maintain a technical blog is to document how I did something, because inevitably I’ll need to do that same thing three more times in my career, but those three times will be so far apart in time that I’ll forget what I did.  Having said that, I maintain no illusions that I write about anything novel here – please assume this is just me being slow, and that I’m giddy about figuring out what the rest of you already knew.  For today’s episode, I’m writing about custom paging with DataGrids.


I’ll admit it.  I rushed.

I needed to build an admin tool that showed a list of items to be approved or rejected, and I decided to just slam a collection into a DataGrid, add a few buttons, and call it a day.  On the off-chance that we got a lot of submissions, I turned on the DataGrid’s default paging so the admin wouldn’t have to scroll through hundreds of entries at a time on one long page.  Ironically, it was the fact that we got hundreds of entries that caused the issue.

The issue was that the admin tool timed out before showing even the first page.  Because I used the grid’s built-in paging controls, it was attempting to bring back the ENTIRE data set every time, but only show one slice of that data (the page requested).  It worked great when I had 15 records in my development database.  Didn’t work out so well when I had 800 (part of each record was a BLOB, so while 800 seems like a small number, there was actually a lot of bytes coming back).

In my rush to get the admin tool out the door, my shortcut turned into a paper cut – small, seemingly innocuous, and very painful.

The solution was to modify the grid and the stored procedure to only bring back and attempt to display one page of records at a time.  The first part was to modify the stored procedure and the .NET code that invokes it to take parameters for the page requested and the number of records to include on each page.  That was relatively straightforward.

The second part was to wire up the DataGrid control to call this stored procedure with the correct page each time.  That required a little more work.

I first handled the grid’s PageIndexChanged event, and called my “Load” method using e.NewPageIndex, the 0-indexed page of data being requested.  The “Load” method would then set the DataGrid.CurrentPageIndex property to that value.  After that I would call my stored procedure and bind the results to the DataGrid as before.

At this point, the grid displayed with a number “1” in the corner as I had intended and the first page of results was showing, but even my development database had more than one page of data.  Where were the other page numbers?  After some reading, especially this post, I found that I needed to set the DataGrid’s VirtualItemCount property to the total number of items in the result set.  That combined with the DataGrid’s PageSize property (which I had already set), it could properly render all the page numbers.

And it did.

Except that clicking on any of them merely returned the first page of data again.  After more reading, I found that there was another property on the DataGrid that needed to be turned on.  I had set the AllowPaging property to True, but I found that I also needed to set the AllowCustomPaging property to True.  Once I did that, the page numbers mapped to the proper page of data.

And a cooling salve was applied to the paper cut.


February 18, 2011 - Posted by | Visual Studio/.NET

Sorry, the comment form is closed at this time.

%d bloggers like this: