locked
Display number of records per page of GridView RRS feed

  • Question

  • User-1605638221 posted

    Hi,

    Is there a way to display number of records per page froma  gridview as below:

    Displaying issues 1 to 50 of 71 matching issues

    So if my page size is set to 50, then then next page should show 51 to 71

    Any suggestions?

    Thanks,

    Navin 

    Monday, October 31, 2011 3:40 PM

Answers

  • User-114806852 posted

    You can use the following example. To simplify things I'm using a generic list of books as the datasource. The important thing to note is that you must return an Id column, because the value is being used in the RowDataBound event.

    <div style="width: 600px">
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="4" 
                ForeColor="#333333" GridLines="None" 
                onpageindexchanging="GridView1_PageIndexChanging" 
                onrowdatabound="GridView1_RowDataBound">
                <PagerSettings FirstPageText="First" LastPageText="Last" 
                    Mode="NextPreviousFirstLast" NextPageText="Next" PreviousPageText="Prev" />
                <RowStyle BackColor="#EFF3FB" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        
            <div style="float: right; width: 145px">
                Showing <asp:Label ID="PageIdxLabel" runat="server" Text="Label" />
                <asp:Label ID="PageRowCountLabel" runat="server" Text="Label" /> of
                <asp:Label ID="PageTotalLabel" runat="server" Text="Label" />
            </div>
    </div>

    public class Book
    {
       public int Id { get; set; }
       public string Title { get; set; }
       public string Author { get; set; }
       public string Publisher { get; set; }
       public int Pages { get; set; }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
       {
           GridView1.DataSource = books;
           GridView1.DataBind();
       }
    }
    
    private static List<Book> books = new List<Book>
    {
       new Book { Id = 1, Title = "Pro ASP.NET 3.5 in C# 2008", Author = "Matthew MacDonald", Publisher = "Apress", Pages = 1498 },
       new Book { Id = 2, Title = "ASP.NET 3.5 Unleashed", Author = "Stephen Walther", Publisher = "Sams", Pages = 1920 },
       new Book { Id = 3, Title = "Beginning ASP.NET 3.5: In C# and VB", Author = "Imar Spaanjaars", Publisher = "Wrox", Pages = 768 },
       new Book { Id = 4, Title = "Programming Microsoft ASP.NET 3.5", Author = "Dino Esposito", Publisher = "Microsoft Press", Pages = 1128 },
       new Book { Id = 5, Title = "Building a Web 2.0 Portal with ASP.NET 3.5", Author = "Omar Al Zabir", Publisher = "O'Reilly Media", Pages = 308 },
       new Book { Id = 6, Title = "Learning ASP.NET 3.5", Author = "Jesse Liberty", Publisher = "O'Reilly Media", Pages = 607 },
       new Book { Id = 7, Title = "ASP.NET 3.5 For Dummies", Author = "Ken Cox", Publisher = "For Dummies", Pages = 404 },
       new Book { Id = 8, Title = "Murach's ASP.NET 3.5 Web Programming with C# 2008", Author = "Anne Boehm", Publisher = "Mike Murach & Associates", Pages = 974 },
       new Book { Id = 9, Title = "ASP.NET 3.5 Website Programming: Problem - Design - Solution", Author = "Chris Love", Publisher = "Wrox", Pages = 700 },
       new Book { Id = 10, Title = "Pro ASP.NET 3.5 Server Controls and AJAX Components", Author = "Rob Cameron", Publisher = "Apress", Pages = 740 },
       new Book { Id = 11, Title = "ASP.NET 3.5 Application Architecture and Design", Author = "Vivek Thakur", Publisher = "Packt Publishing", Pages = 260 },
       new Book { Id = 12, Title = "Pro ASP.NET 3.5 in VB 2008: Includes Silverlight 2", Author = "Matthew MacDonald", Publisher = "Apress", Pages = 1500 },
       new Book { Id = 13, Title = "ASP.NET 3.5 AJAX Unleashed", Author = "Robert Foster", Publisher = "Sams", Pages = 264 },
       new Book { Id = 14, Title = "ASP.NET 3.5 : A Beginner's Guide", Author = "William Sanders", Publisher = "McGraw-Hill Osborne Media", Pages = 480 },
       new Book { Id = 15, Title = "ASP.NET AJAX in Action", Author = "Alessandro Gallo", Publisher = "Manning Publications", Pages = 600 },
       new Book { Id = 16, Title = "Windows Presentation Foundation Unleashed", Author = "Adam Nathan", Publisher = "Sams", Pages = 656 },
       new Book { Id = 17, Title = "Programming WPF", Author = "Chris Sells", Publisher = "O'Reilly Media", Pages = 863 },
       new Book { Id = 18, Title = "Learning WCF: A Hands-on Guide", Author = "Michele Bustamante", Publisher = "O'Reilly Media", Pages = 607 },
       new Book { Id = 19, Title = "Programming WCF Services", Author = "Juval Lowy", Publisher = "O'Reilly Media", Pages = 783 },
       new Book { Id = 20, Title = "Pro WCF: Practical Microsoft SOA Implementation", Author = "Amit Bahree", Publisher = "Apress", Pages = 512 },
       new Book { Id = 21, Title = "Pro LINQ: Language Integrated Query in C# 2008", Author = "Jr., Joseph C. Rattz", Publisher = "Apress", Pages = 600 },
       new Book { Id = 22, Title = "LINQ Unleashed: for C#", Author = "Paul Kimmel", Publisher = "Sams", Pages = 552 },
       new Book { Id = 23, Title = "Pro ASP.NET MVC 3 Framework", Author = "Steven Sanderson", Publisher = "Apress", Pages = 852 },
       new Book { Id = 24, Title = "Professional ASP.NET MVC 3", Author = "Jon Galloway", Publisher = "Wrox", Pages = 432 },
       new Book { Id = 25, Title = "Test-Drive ASP.NET MVC", Author = "Jonathan McCracken", Publisher = "Pragmatic Bookshelf", Pages = 296 }
    };
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       PageIdxLabel.Text = e.NewPageIndex + " - ";
       PageRowCountLabel.Text = GridView1.Rows.Count.ToString(); 
       PageTotalLabel.Text = books.Count.ToString(); 
       GridView1.PageIndex = e.NewPageIndex;
       GridView1.DataSource = books;
       GridView1.DataBind();
    }
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       foreach (GridViewRow row in GridView1.Rows)
       {
           PageIdxLabel.Text = GridView1.Rows[0].Cells[0].Text + " - ";
           PageRowCountLabel.Text = row.Cells[0].Text;
       }
    
       PageTotalLabel.Text = books.Count.ToString();
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 5, 2011 12:26 PM

All replies

  • User-608283337 posted

    you can achieve this by setting the following properties of your gridview in design time. hope this helps.

    PageSize="50" AllowPaging="True"

    Monday, October 31, 2011 4:02 PM
  • User-1605638221 posted

    Thanks for hte update but how can get the other part i.e. to show the furst and last record number.

    So if on page 1 then, it should display Showing 1 to 50 of 100

    When on Page 2 then, it should display Showing 51 to 100 of 100

     

    Thanks,

    Navin

    Monday, October 31, 2011 4:34 PM
  • User-608283337 posted

    I see. You want custom paging. refer the link below to find different options/solutions.

     

    http://forums.asp.net/t/1608423.aspx/1

    http://www.codeproject.com/KB/webforms/GridViewCustomPaging.aspx

     

     

    Monday, October 31, 2011 4:45 PM
  • User-114806852 posted

    You can use the following example. To simplify things I'm using a generic list of books as the datasource. The important thing to note is that you must return an Id column, because the value is being used in the RowDataBound event.

    <div style="width: 600px">
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="4" 
                ForeColor="#333333" GridLines="None" 
                onpageindexchanging="GridView1_PageIndexChanging" 
                onrowdatabound="GridView1_RowDataBound">
                <PagerSettings FirstPageText="First" LastPageText="Last" 
                    Mode="NextPreviousFirstLast" NextPageText="Next" PreviousPageText="Prev" />
                <RowStyle BackColor="#EFF3FB" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        
            <div style="float: right; width: 145px">
                Showing <asp:Label ID="PageIdxLabel" runat="server" Text="Label" />
                <asp:Label ID="PageRowCountLabel" runat="server" Text="Label" /> of
                <asp:Label ID="PageTotalLabel" runat="server" Text="Label" />
            </div>
    </div>

    public class Book
    {
       public int Id { get; set; }
       public string Title { get; set; }
       public string Author { get; set; }
       public string Publisher { get; set; }
       public int Pages { get; set; }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
       {
           GridView1.DataSource = books;
           GridView1.DataBind();
       }
    }
    
    private static List<Book> books = new List<Book>
    {
       new Book { Id = 1, Title = "Pro ASP.NET 3.5 in C# 2008", Author = "Matthew MacDonald", Publisher = "Apress", Pages = 1498 },
       new Book { Id = 2, Title = "ASP.NET 3.5 Unleashed", Author = "Stephen Walther", Publisher = "Sams", Pages = 1920 },
       new Book { Id = 3, Title = "Beginning ASP.NET 3.5: In C# and VB", Author = "Imar Spaanjaars", Publisher = "Wrox", Pages = 768 },
       new Book { Id = 4, Title = "Programming Microsoft ASP.NET 3.5", Author = "Dino Esposito", Publisher = "Microsoft Press", Pages = 1128 },
       new Book { Id = 5, Title = "Building a Web 2.0 Portal with ASP.NET 3.5", Author = "Omar Al Zabir", Publisher = "O'Reilly Media", Pages = 308 },
       new Book { Id = 6, Title = "Learning ASP.NET 3.5", Author = "Jesse Liberty", Publisher = "O'Reilly Media", Pages = 607 },
       new Book { Id = 7, Title = "ASP.NET 3.5 For Dummies", Author = "Ken Cox", Publisher = "For Dummies", Pages = 404 },
       new Book { Id = 8, Title = "Murach's ASP.NET 3.5 Web Programming with C# 2008", Author = "Anne Boehm", Publisher = "Mike Murach & Associates", Pages = 974 },
       new Book { Id = 9, Title = "ASP.NET 3.5 Website Programming: Problem - Design - Solution", Author = "Chris Love", Publisher = "Wrox", Pages = 700 },
       new Book { Id = 10, Title = "Pro ASP.NET 3.5 Server Controls and AJAX Components", Author = "Rob Cameron", Publisher = "Apress", Pages = 740 },
       new Book { Id = 11, Title = "ASP.NET 3.5 Application Architecture and Design", Author = "Vivek Thakur", Publisher = "Packt Publishing", Pages = 260 },
       new Book { Id = 12, Title = "Pro ASP.NET 3.5 in VB 2008: Includes Silverlight 2", Author = "Matthew MacDonald", Publisher = "Apress", Pages = 1500 },
       new Book { Id = 13, Title = "ASP.NET 3.5 AJAX Unleashed", Author = "Robert Foster", Publisher = "Sams", Pages = 264 },
       new Book { Id = 14, Title = "ASP.NET 3.5 : A Beginner's Guide", Author = "William Sanders", Publisher = "McGraw-Hill Osborne Media", Pages = 480 },
       new Book { Id = 15, Title = "ASP.NET AJAX in Action", Author = "Alessandro Gallo", Publisher = "Manning Publications", Pages = 600 },
       new Book { Id = 16, Title = "Windows Presentation Foundation Unleashed", Author = "Adam Nathan", Publisher = "Sams", Pages = 656 },
       new Book { Id = 17, Title = "Programming WPF", Author = "Chris Sells", Publisher = "O'Reilly Media", Pages = 863 },
       new Book { Id = 18, Title = "Learning WCF: A Hands-on Guide", Author = "Michele Bustamante", Publisher = "O'Reilly Media", Pages = 607 },
       new Book { Id = 19, Title = "Programming WCF Services", Author = "Juval Lowy", Publisher = "O'Reilly Media", Pages = 783 },
       new Book { Id = 20, Title = "Pro WCF: Practical Microsoft SOA Implementation", Author = "Amit Bahree", Publisher = "Apress", Pages = 512 },
       new Book { Id = 21, Title = "Pro LINQ: Language Integrated Query in C# 2008", Author = "Jr., Joseph C. Rattz", Publisher = "Apress", Pages = 600 },
       new Book { Id = 22, Title = "LINQ Unleashed: for C#", Author = "Paul Kimmel", Publisher = "Sams", Pages = 552 },
       new Book { Id = 23, Title = "Pro ASP.NET MVC 3 Framework", Author = "Steven Sanderson", Publisher = "Apress", Pages = 852 },
       new Book { Id = 24, Title = "Professional ASP.NET MVC 3", Author = "Jon Galloway", Publisher = "Wrox", Pages = 432 },
       new Book { Id = 25, Title = "Test-Drive ASP.NET MVC", Author = "Jonathan McCracken", Publisher = "Pragmatic Bookshelf", Pages = 296 }
    };
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       PageIdxLabel.Text = e.NewPageIndex + " - ";
       PageRowCountLabel.Text = GridView1.Rows.Count.ToString(); 
       PageTotalLabel.Text = books.Count.ToString(); 
       GridView1.PageIndex = e.NewPageIndex;
       GridView1.DataSource = books;
       GridView1.DataBind();
    }
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       foreach (GridViewRow row in GridView1.Rows)
       {
           PageIdxLabel.Text = GridView1.Rows[0].Cells[0].Text + " - ";
           PageRowCountLabel.Text = row.Cells[0].Text;
       }
    
       PageTotalLabel.Text = books.Count.ToString();
    }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 5, 2011 12:26 PM
  • User-1605638221 posted

    Thanks

    Saturday, November 5, 2011 3:24 PM