Library tutorials & articles

Custom ASP.NET Datagrid Paging With Exact Count

Introduction

Anybody in the DB world knows what paging database results is and its effect. From the time I had started getting into good old classic ASP, I was intrigued with the ability to divide large sets of data into sections of x records per page. One thing that I didn't like about paging is that it seemed sites incorporated just a < Prev and Next > link on the search results page. I wasn't satisfied with such a lackluster paging technique, and from there I searched high and low on every ASP Web site I could find to see if there was code to show more advanced paging options, such as how many pages were remaining to be paged through, or, if the next page was the last page of results, how many records were on that last page. Unfortunately, I couldn't find any such code, so I had set out to do it myself. (To see the proposed paging enhancements I like, check out the live demo for this article...)

I have since coded a number of techniques for advanced paging in classic ASP, but my latest challenge has been to incorporate the same paging techniques in ASP.NET! (For more information on ASP.NET, be sure to visit the ASP.NET Article Index.) Pssst, don't ask me to talk about redoing my app in Beta 1, and upon upgrading to Beta 2 was horrified that my code needed to be redone.... by the way, the code in this article is all Beta 2 compliant.

Now, if anyone has looked into the Microsoft .NET SDK and Quickstart samples you will find custom paging samples, but it's the usual next and prev stuff. Now let's see how we can kick this paging up a notch and tell us more detail about our data output.

This article was originally published on 4guysfromrolla.com.

Comments

  1. 08 Jul 2005 at 17:18

    Hi, thanks, I appreciate your compliments.


    I have written another older article that I think should help. It's entitled- Drilldown Datagrid Searching with ASP.NET.


    This article demonstrates how to do a standard search, display the results in a datagrid, then further filter and search the datagrid itself and drilldown into the datagrid results themselves.


    So to merge the two, work off of the aforementioned article and incorporate the custom paging. All this should be no problem, and plenty to get you up and running.


    Also, for scalability purposes, read another article of mine here on DF - .NET Data Caching . Between the two you'll be able to provide some great functionality and performance.


    I hope this helps!


    - Jimmy Markatos

  2. 08 Jul 2005 at 15:56

    I loved your article on custom paging but would very much like to know how to apply it when you filter your initial data set to retreive specific records. I have a grid with 813 records sorted by user name. I have a search box in which a user can enter a name in order to filter the original dataset. If the search result in only 4 pages of records how can I page through that new set of records? Thanks in advance for any help and for the great article.

  3. 26 Oct 2003 at 20:00

    Check this site...


    I too was on the same quest and I've created a best practices reference site that covers many enterprise topics necessary to create large scale enterprise sites.

  4. 17 Oct 2003 at 05:38
    Hello,

    Tankete, I just have try what you are asking for a couple of days ago :

    The principe is simple : returning only a needed page from 50th to 60th records consists in querying this way :

    SELECT * TOP 60 ... WHERE NOT IN SELECT TOP 50 * WHERE [joining primarykeys]

    I have taken advices from Dino Esposito's MSDN articles, and have created a PageQuery function thats returns such a SQL statement, based on a normal SQL statement, primaryKey information, currentpageindex and pagesize.

    This sample of code uses a custompaged enabled datagrid called dg, with a access database that contains a "HugeTable" table with a "ID" primary key.

    Do not forget to enable custompaging to your datagrid

    Code:


       Private _Cx As OleDbConnection

       Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

           _Cx = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:\Inetpub\wwwroot\labo\app\db\paged_datagrid.mdb;Mode=Share Deny None;")

           If Not IsPostBack Then
               dg.DataBind()
               DataList1.DataBind()
           End If

       End Sub

       '

       Private Sub dg_DataBinding(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dg.DataBinding

           Dim sqlstr As String = PageQuery("SELECT * FROM HugeTable ORDER BY ID", "ID", dg.CurrentPageIndex, dg.PageSize)
           _Cx.Open()

           dg.VirtualItemCount = New OleDbCommand("SELECT COUNT(*) FROM HugeTable", _Cx).ExecuteScalar
           Dim dr As OleDbDataReader = New OleDbCommand(sqlstr, _Cx).ExecuteReader(CommandBehavior.CloseConnection)
           dg.DataSource = dr

       End Sub

       '
       '
       Private Function PageQuery(ByVal commandText As String, _
                                  ByVal primaryKey As String, _
                                  ByVal currentPageIndex As Integer, _
                                  ByVal pageSize As Integer) As String

           Dim SubQuery1_TopCount As Integer = IIf(currentPageIndex = 0, _
                                                   pageSize, _
                                                   currentPageIndex * pageSize + pageSize)

           Dim SubQuery1 As String = Replace(commandText, _
                                             "SELECT ", _
                                             String.Format("SELECT TOP {0} ", SubQuery1_TopCount))

           If currentPageIndex = 0 Then Return SubQuery1

           SubQuery1 = String.Format("({0}) AS T1", SubQuery1)

           Dim SubQuery2_TopCount As Integer = currentPageIndex * pageSize

           Dim SubQuery2 As String = Replace(commandText, _
                                             "SELECT ", _
                                             String.Format("SELECT TOP {0} ", SubQuery2_TopCount))

           SubQuery2 = String.Format("({0}) AS T2", SubQuery2)

           Dim MainQuery As String = String.Format("SELECT t1.* FROM {0} LEFT JOIN {1} ON T1.{2}= T2.{2} WHERE T2.{2} IS NULL", _
                                                   SubQuery1, _
                                                   SubQuery2, _
                                                   primaryKey)

           Return MainQuery

       End Function

       '

       Private Sub dg_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dg.PageIndexChanged
           dg.CurrentPageIndex = e.NewPageIndex
           dg.DataBind()
       End Sub



    Hope it helps. For SQL server, the built query could be better implemented using subqueries instead of inner join, which is much more faster in access.

    Laurent.
  5. 16 Oct 2003 at 14:58
    I was hoping that you would address how to do paging with exact counts while NOT returning the entire dataset from the database, just the current page worth of data. Can you provide any information on how you would accomplish that? The current approach that I can think of does not perform as well as desired.
  6. 01 Jan 1999 at 00:00

    This thread is for discussions of Custom ASP.NET Datagrid Paging With Exact Count.

Leave a comment

Sign in or Join us (it's free).

Dimitrios Markatos Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and deskto...
AddThis

Related podcasts

Events coming up

  • Jul 7

    DTC 70-528 Session 7: Chapter 12

    Greenwood Village, United States

    Due to lack of interest of the 5th Monday meetup, we will continue as originally scheduled. The topic of the night will be "Chapter 12 - Creating ASP.NET Mobile Web Applications", taught by RJ Hatch. It is a fairly small chapter, so we can discuss other topics as well. Pizza and beverages will be provided on a donation basis.

Want to stay in touch with what's going on? Follow us on twitter!