• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

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

0
Justice75
Asked:
Justice75
  • 5
  • 3
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now