Return of the DataGrid: Paging and Editing Explained - Paging
(Page 2 of 4 )
“Umm, what is paging?” you ask. Simply put, it is the means by which we can apportion our data into chunks and deliver them a piece at a time. A good example of this is a Google search. By default you receive 10 search results at a time, or 'per page'. Then you have the ability to see the next ten results, the next 'page' of results.
We can and should apply the same methodology in our applications. Why? Well, suppose you're building an application to manage inventory. What are the chances of having only four pieces of inventory in total? I would guess that it's roughly about the same probability of me finding a million bucks in the mail tomorrow. Not null, just very improbable. For example, an inventory app I worked on for a company of only 200 employees has thousands of entries being tracked. Picture surfacing all entries at once, and you quickly understand the usefulness of paging. The same holds true for forums, e-commerce website, etc.
With ASP, creating paging involved a mix of server and client-side scripting, and was just a pain to implement, maintain, and reuse. ASP.Net solves all that. In fact, you won't believe just how easy this is. I'm hoping that you still have the basic code in mind or in an IDE to work with because I'm not going to retype it; I'm just going to start adding in the new pieces. The first thing we do is modify our DataGrid itself:
<asp:DataGrid id="dgSocks" runat="server"
AllowPaging="true" OnPageIndexChanged="pager" PageSize="3" >
This is fairly straightforward. We are just saying that we want to allow paging, what exactly we want to do when the page index changes (a subroutine called “pager”), and defining the default number of results to return per page.
You can combine this with any other options, such as sorting. But just make sure that you have set the AutoGenerateColumns property to to true, so that .NET knows to handle it.
It's good to know exactly what is going to be produced here. Basically, you can expect an additional row added to th bottom of your table (grid). At the left side of the row will be two arrows (< & >) indicating that you can advance or retreat to another page. Now most likely you are as unsatisfied with default settings as I am, so thankfully we have a good degree of customizability (I think I may have just invented a word) available to us. We could easily center the arrows by adding the following:
PagerStyle
-HorizontalAlign="center"
We may also come to the conclusion that the arrows just won't cut it, we want to know exactly where we are, we want numbered pages. Well hold on to your hat, because you're about to be blown away by the immense amount of extra code thats required:
PagerStyle
-Mode="NumericPages"
Wow, was that ever difficult?! It took all of let's see, about 30 characters? I can almost hear you singing your paean to Microsoft already! Oh, but it gets better still; let me show you the editing capabilities!
Next: Editing >>
More .NET Articles
More By Justin Cook