Professional Web Applications Themes

Getting Total Item Count in Paging Datagrid - ASP.NET Data Grid Control

I'm not sure if I am just missing some obvious function call or what. I want to be able to get the overall count of the records returned from my database query. When I use datagrid.Item.Count, it only returns the count for the current page. I see a property that lists pagecount, but nothing for totalcount. I want to have a label above my datagrid that says "n results returned". Can anyone help, thanks. *** Sent via Developersdex [url]http://www.developersdex.com[/url] *** Don't just participate in USENET...get rewarded for it!...

  1. #1

    Default Getting Total Item Count in Paging Datagrid



    I'm not sure if I am just missing some obvious function call or what.

    I want to be able to get the overall count of the records returned from
    my database query. When I use datagrid.Item.Count, it only returns the
    count for the current page. I see a property that lists pagecount, but
    nothing for totalcount. I want to have a label above my datagrid that
    says "n results returned".

    Can anyone help, thanks.

    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    jason smith Guest

  2. #2

    Default Re: Getting Total Item Count in Paging Datagrid

    Hi

    Use dataGrid.PageCount * dataGrid.PageSize

    HTH
    Prasad

    "jason smith" <dontemail22.com> wrote in message
    news:OGODxpBUDHA.1912tk2msftngp13.phx.gbl...
    >
    >
    > I'm not sure if I am just missing some obvious function call or what.
    >
    > I want to be able to get the overall count of the records returned from
    > my database query. When I use datagrid.Item.Count, it only returns the
    > count for the current page. I see a property that lists pagecount, but
    > nothing for totalcount. I want to have a label above my datagrid that
    > says "n results returned".
    >
    > Can anyone help, thanks.
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    Prasad Guest

  3. #3

    Default Re: Getting Total Item Count in Paging Datagrid



    That solution will not give me the exact number. For example if the user
    has paging set to 100 items per page and there are 577 items returned,
    your solution will display 600 items found.

    Is there a way to do (pagecount-1 * numberofpages) + (lastpagecount).


    Thanks,

    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    jason smith Guest

  4. #4

    Default Re: Getting Total Item Count in Paging Datagrid

    get it from the underlying datagrid
    datagrid.table[0].rows.count will always be accurate.

    "jason smith" <dontemail22.com> wrote in message
    news:uUrecVHUDHA.940TK2MSFTNGP11.phx.gbl...
    >
    >
    > That solution will not give me the exact number. For example if the user
    > has paging set to 100 items per page and there are 577 items returned,
    > your solution will display 600 items found.
    >
    > Is there a way to do (pagecount-1 * numberofpages) + (lastpagecount).
    >
    >
    > Thanks,
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    Alvin Bruney Guest

  5. #5

    Default Re: Getting Total Item Count in Paging Datagrid

    sorry i meant dataset. duh...brain .

    "Alvin Bruney" <vapordan_spam_me_nothotmail_no_spamhotmail.com > wrote in
    message news:#MWHrDNUDHA.3024tk2msftngp13.phx.gbl...
    > get it from the underlying datagrid
    > datagrid.table[0].rows.count will always be accurate.
    >
    > "jason smith" <dontemail22.com> wrote in message
    > news:uUrecVHUDHA.940TK2MSFTNGP11.phx.gbl...
    > >
    > >
    > > That solution will not give me the exact number. For example if the user
    > > has paging set to 100 items per page and there are 577 items returned,
    > > your solution will display 600 items found.
    > >
    > > Is there a way to do (pagecount-1 * numberofpages) + (lastpagecount).
    > >
    > >
    > > Thanks,
    > >
    > > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > > Don't just participate in USENET...get rewarded for it!
    >
    >

    Alvin Bruney Guest

  6. #6

    Default Re: Getting Total Item Count in Paging Datagrid (SOLUTION)

    It would be simple if we could do as follows to get total number of items (rows) in a datagrid:

    datagridName.Items.Count

    But this only counts the items in the current page.

    On the other hand, multiply the PageSize by the PageCount is no solution because the last page will not necessarily have the total number of items defined in PageSize. As an example imagine this: 12 items in a datagrid, PageSize=10, so we will have 2 pages (the first page with 10 items and the second with only 2 items).

    The solution is to subtract one page (the last page which we never know how many items it has) then multiply the PageSize by the PageCount (all other pages are full) and finally add the number of items from the last page. This works even if there is only one page as you will see.

    So it would be like this:

    (datagridName.PageCount - 1) * (datagridName.PageSize) + datagridName.Items.Count

    In my previous example of a datagrid with 12 items the result would be:

    (2-1) * (10) + 2 = 1 * 10 + 2 = 10 + 2 = 12 (that is correct!)

    And if my PageSize was 20 and so I would have only one page for the 12 items:

    (1-1) * (20) + 12 = 0 * 20 + 12 = 0 + 12 = 12 (that is correct!)

    We just have to make sure the "datagridName.Items.Count" part of our formula gets the items from the last page and not any other page's items. For that we can set the datagridName.CurrentPageIndex temporarily to the last page (datagridName.CurrentPageIndex = datagridName.PageCount - 1 , where we subtract 1 because we have zero based paging) and then set back the previous value for CurrentPageIndex after getting the result we want.

    I hope this helps everyone that search a solution for this question!
    Good work!

    Rui Belo Pais
    Rui Belo Pais Guest

  7. #7

    Arrow Re: Getting Total Item Count in Paging Datagrid

    Hi, just let me add something to my own post: we have to DataBind our grid after changing to the last page to get the real number of items on that page and DataBind again after returning to initial page to get the initial data.
    Rui Belo Pais Guest

Similar Threads

  1. Verity total doent count
    By Tom Jordahl in forum Coldfusion Server Administration
    Replies: 0
    Last Post: May 17th, 02:27 PM
  2. ASP Group Same Records and Give Total Count
    By gotcha in forum ASP Database
    Replies: 1
    Last Post: August 6th, 08:50 PM
  3. Show DataGrid Item Count?
    By Allen Davis in forum ASP.NET Data Grid Control
    Replies: 3
    Last Post: May 5th, 02:01 PM
  4. total record count in datagrid
    By DC Gringo in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: March 4th, 08:32 PM
  5. Any serverscript to count total images in a folder
    By sraval in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: February 10th, 05:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139