February 27, 2021
Hot Topics:

.NET Data Secrets, Part III

  • By Karl Moore
  • Send Email »
  • More Articles »

Welcome to the third and final part of .NET Data Secrets!

I'm Karl Moore and today we'll continue last week's theme of top-notch data access through the Web DataGrid control, with even more tips and tricks to make your site look the business:

  • Quick and Easy Data Paging, with Your Web Grid
  • Sorting in Seconds, with Your Web Grid
  • Amazingly Simple Method for Exporting Your Web Grid to Excel
  • Get Microsoft to Write Your Code—Amazing Undocumented SQL Server Tool!

But, hold on, this is the final part of .NET Data Secrets? It certainly is—and the eleventh in the entire .NET secrets series we're running on this site. That means the next will be the twelfth and absolute final article in this series, our little look at the world of Web service secrets.

Don't forget: you've still got time to submit your favorite .NET snippets for publication. Simply mail your code and hot techniques to me—karl@karlmoore.com—and I'll publish the best, with full credit.

But enough small talk. Let's get on with those secrets!

Quick and Easy Data Paging, with Your Web Grid

Most Web developers tend to shiver at the mere mention of the word "paging." Even ASP.NET programmers are a little confused over its implementation. Many of the new .NET books devote a chapter or so to the subject—but it really isn't that complicated.

Paging is a method of splitting results in your DataGrid over multiple "pages." Google uses paging in its search results. Yahoo! uses paging if it can't fit all its matching sites onto one page. It's useful—and can be set up in seconds.

First off, setup your DataGrid as usual (see the "Nine Steps to a Quick, Editable Web Grid" tip in the last article if you're a little unsure). Add all the binding code to respond to the page Load event as normal. When you're ready, right-click the grid and select Property Builder. Choose the Paging property sheet and check 'Allow paging.' Alter the 'Page size' so it represents the number of items you want to display on any one 'page.' You may also want to change the navigation text just below. Click OK when finished.

Next, add code similar to the following to respond to the PageIndexChanged event of your DataGrid control:

    MyDataGrid.CurrentPageIndex = e.NewPageIndex
    ' Bind your data again here. You could load DataSet
    ' from ViewState, database, etc. For example:

And that's it—just fire off your DataGrid as normal and you've got instant paging!

Top Tip: If you're going to use editing with paging, watch out when updating your DataSet. If you're doing it purely on position, remember, after you move to a new page, your first item will not correlate with the first item in your DataSet. Remember that - you'll have to code around this. You can find out the current page by looking up the .CurrentPageIndex property, and find out the number of items displayed per page using the .PageSize property.

Page 1 of 3

This article was originally published on June 2, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date