?
Solved

GridView - Pagination - Total Record Count

Posted on 2009-05-07
7
Medium Priority
?
3,886 Views
Last Modified: 2013-11-07
I am databinding a GridView to a DataTable, via an ObjectDataSource.  I have enabled pagination and would like to display pagination data on the GUI as follows:

Displaying [Index of 1st Record on GridView.Page] - [Index of last Record on GridView.Page] of [GridView's total record count] records.

I can calculate the Index of the 1st record:
((this.GridView1.PageIndex + 1) * this.GridView1.PageSize) - (this.GridView1.PageSize - 1);

I can calcualte the Index of the Last Record displayed on the current Page:
((this.GridView1.PageIndex + 1) * this.GridView1.PageSize);

How can I calculate the total record count?

Thanks,

PJ
0
Comment
Question by:ppittle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Assisted Solution

by:urir10
urir10 earned 1000 total points
ID: 24329170
Can you use the dataTable?

string totalNumberOfRows = DataTable.Rows.Count.ToString();

Should give you the total number of rows.
0
 
LVL 10

Expert Comment

by:Christian de Bellefeuille
ID: 24329237
here's what you asked...
--- Code behind file ---
Public Partial Class GridView
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
    End Sub
 
    Private Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
        CType(GridView1.FooterRow.FindControl("Label1"), Label).Text = GridView1.Rows.Count
 
    End Sub
End Class
 
--- Aspx file ---
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="GridView.aspx.vb" Inherits="Test_ASP.NET_App.GridView" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"  DataSourceID="ds1" ShowFooter="True">
        <Columns>
            <asp:TemplateField>
                <FooterTemplate>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        
        </asp:GridView> 
    
        <asp:SqlDataSource ID="ds1" runat="server" ConnectionString="<%$ ConnectionStrings:csMyConnection%>"
                 SelectCommand="SELECT * FROM tblTest">
        </asp:SqlDataSource>    
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 7

Assisted Solution

by:urir10
urir10 earned 1000 total points
ID: 24329257
Wouldnt that give the number of rows of the current page of the gridview?
0
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
LVL 10

Expert Comment

by:Christian de Bellefeuille
ID: 24329274
yeah, sorry, i've just added the pagination and noticed that it's only rows on the actual page.
0
 
LVL 10

Accepted Solution

by:
Christian de Bellefeuille earned 1000 total points
ID: 24329397
You can change the code behind with what's bellow.  It work
Public Partial Class GridView
    Inherits System.Web.UI.Page
 
    Private RecordsCount As Long
 
 
    Private Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
        CType(GridView1.FooterRow.FindControl("Label1"), Label).Text = RecordsCount
    End Sub
 
 
    Private Sub ds1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles ds1.Selected
        RecordsCount = e.AffectedRows
    End Sub
End Class

Open in new window

0
 
LVL 8

Author Comment

by:ppittle
ID: 24330429
Thanks for all the feed back!  After reviewing your comments and tinkering a bit I came up with this, which works:

protected void AllStoriesGridView_DataBound(object sender, EventArgs e)
        {    
            PaginationRecordCountLabel.Visible = (AllStoriesGridView.Rows.Count > 0);

            string recordCountFormatString = "Displaying records {0}-{1} out of {2}.";

            ObjectDataSource ods = (ObjectDataSource)AllStoriesGridView.DataSourceObject;
            int totalRowsInDataSource = (ods.Select() as DataView).Count;

            int firstRecordOnPageIndex = ((AllStoriesGridView.PageIndex + 1) * AllStoriesGridView.PageSize) - AllStoriesGridView.PageSize + 1;
            int lastRecordOnPageIndex;

            if (((AllStoriesGridView.PageIndex + 1) * AllStoriesGridView.PageSize) > totalRowsInDataSource)
            {
                lastRecordOnPageIndex = totalRowsInDataSource;
            }
            else
            {
                lastRecordOnPageIndex = ((AllStoriesGridView.PageIndex + 1) * AllStoriesGridView.PageSize);
            }            

            PaginationRecordCountLabel.Text = string.Format(recordCountFormatString,
                firstRecordOnPageIndex,
                lastRecordOnPageIndex,
                totalRowsInDataSource);
}


cdebel and urir10 I tried this out too:

 protected void AllStoriesDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            int totalRowsInDataSource = (e.ReturnValue as DataTable).Rows.Count;
        }

Which is similiar to what the two of you suggested.  However, this doesn't take into account any filtering done on the ObjectDataSource, which, granted, wasn't part of my original problem description.  It does, however, return the number of rows being returned from the DB.

I also looked at e.AffectedRows (where e is ObjectDataSourceStatusEventArgs), and it always returned -1 for some reason.  Might be some odd difference between ObjectDataSources and SqlDataSources.  

Regardless, thank you both for the assistance.

PJ
0
 
LVL 8

Author Closing Comment

by:ppittle
ID: 31579095
Note:  I posted the solution that worked in my case (ObjectDataSources).  cdebel and urir10's comment's are only marked as solution in order to award points for their assistance.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question