Displaying Records Numbers and Page Numbers for Data Grid

Hi All,

I want to be able to add these values to a text box and to the DataGrid on the same row with the pager (if possible). :

Example:
Displaying Records 81 - 120 of 252  Page 4 of 40

whereas: 81 (first record of current page)
120 (last record of current page)
252 (total records in query
4 (current page)
40 (total pages)

I need the values to change as the pageIndex changes. I am using a  custom sorting and paging mechanism that uses the default names for the parameters of the ObjectDataSource:  I am using a BLL, DAL, stored procedures and C# for the page.

maximumRows, startRowIndex and sortExpression

I am using the code behind file for the total records with no problem.  That code is attached.


I used this code but it caused undesirable results that only seemed to work for the first page:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_21749099.html

int intStartRecord = (GridView1.PageIndex * GridView1.PageSize) + 1;
int intEndRecord = (GridView1.PageIndex * GridView1.PageCount) + 1 + GridView1.Rows.Count;
int intCurrentPage = GridView1.PageIndex + 1;
int intTotalRecords = YourDataSource.Rows.Count;

lblDisplaying.Text = "Displaying records " + intStartRecord.ToString() + " through " + intEndRecord.ToString() + " of " + intTotalRecords.ToString() + " items found (page " + intCurrentPage.ToString() + " of " + GridView1.PageCount.ToString() + ")!";




Thanks for any help!

Jus
Total Number of Records - Code Behind File (C#)
==============================================================
    protected void GridView1_Load(object sender, EventArgs e)
    {
        MakesBLL bll = new MakesBLL(); 
        int rowCount = bll.TotalNumberOfMakes();
        GridView1.Columns[2].FooterText = "Total : "  + rowCount.ToString();
        TextBox1.Text ="Total Records: " + rowCount.ToString();
    }

=========================================================
ObjectDataSource
=========================================================

        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            SelectMethod="GetMakesPagesAndSorted" 
            TypeName="MakesBLL" 
            EnablePaging="True" 
            OldValuesParameterFormatString="original_{0}" 
            SelectCountMethod="TotalNumberOfMakes" 
            SortParameterName="sortExpression">
            <SelectParameters>
            </SelectParameters>            
        </asp:ObjectDataSource> 

==========================================================
BLL - sortExpression, startRowIndex, maximumRows
==========================================================
    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public DA2.car_MakesDataTable GetMakesPagesAndSorted(string sortExpression, int startRowIndex, int maximumRows)
    {
        return Adapter.GetMakesPagesAndSorted(sortExpression, startRowIndex, maximumRows);

    }

===========================================================
BLL - total Records
===========================================================

 public int TotalNumberOfMakes()
    {
        return Adapter.TotalNumberOfMakes().GetValueOrDefault();
    }

Open in new window

Justice75Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

strickddCommented:
You just need to create your own custom PageTemplate for the gridview. Here is a good tutorial that should help: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pagertemplate.aspx
0
Daniel JungesCommented:
place at the botton from gridview the follow:
                         <% if (dataGridView1.Rows.Count > 0)
                           {%>
                        <i>Página nr.°
                            <%=dataGridView1.PageIndex + 1%>
                            de
                            <%=dataGridView1.PageCount%>
                        </i>
                        <% } %>
0
Justice75Author Commented:
Thamsk guys.  I need something though that is going to show me how to access the current start record for  the page and the end record for the page.


Jus
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

strickddCommented:
if you know how many you're displaying per page, and you know what page you're on, it is a simple calculation to figure out the range.

((PageNum - 1) * NumPerPage + 1) = MinRecordNumber
MinRecordNumber + NumPerPage = MaxRecordNumber


If you need to display the ID range of the data, you can pull that from the GridView control by getting the ID's for the following rows:

GridViewControl.Rows[0]
GridViewControl.Rows[NumPerPage - 1]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Justice75Author Commented:
strickdd,

How will I know what page that I am on.  Do you have a sample of exactly what I need to do?

Jus
0
strickddCommented:
GridViewControl.PageIndex + 1
0
Justice75Author Commented:
This what I have so far but I am not getting the " Record X - X of X".... part


 protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        // Call a helper method to display the current page number
        // when the user navigates to a different page.
        DisplayCurrentPage();
    }

    void DisplayCurrentPage()
    {
        // Calculate the current page number.
        int currentPage = GridView1.PageIndex + 1;

        // Display the current page number.
        TextBox2.Text = "Page " + currentPage.ToString() + " of " +
        GridView1.PageCount.ToString() + ".";
    }
0
Justice75Author Commented:
got me on the right road.. I am thankful...
0
Justice75Author Commented:
For anyone that is going down the same road that I was on for 5 days.. Here is the complete code from pieces here and there using an ObjectDataSource:


:
protected void GridView1_DataBound(object sender, EventArgs e)
    {
        DisplayPager();
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e) 
   {
       DisplayPager();
	}

    void DisplayPager()
    {
        YourBLL bll = new YourBLL();
        int rowCount = bll.YourQueryInBLL();
        int iTotalRecords = rowCount;
        int iEndRecord = GridView1.PageSize * (GridView1.PageIndex + 1);
        int iStartRecord = iEndRecord - GridView1.PageSize;
        // Calculate the current page number.
        int currentPage = GridView1.PageIndex + 1;

        if (iEndRecord > iTotalRecords)
        {
            iEndRecord = iTotalRecords;
        }

        if (iStartRecord == 0)
        {
            iStartRecord = 1;

        }
        else
        {
            iStartRecord = iStartRecord + 1;
        }

        if (iEndRecord == 0)
        {
            iEndRecord = iTotalRecords;
        }

        TxtPageCount.Text = iStartRecord.ToString() + " to " + iEndRecord.ToString() + " of " + iTotalRecords.ToString() + "        Page" + currentPage.ToString() + " of " + GridView1.PageCount.ToString();

    }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.