Click here to Skip to main content
15,884,040 members
Articles / Web Development / ASP.NET

A very nice and complete custom GridView pager with no ViewState

Rate me:
Please Sign up or sign in to vote.
3.73/5 (8 votes)
14 Oct 2007CPOL5 min read 76.4K   979   52  
How to implement a custom pager for the GridView without relying on ViewState or the GridView's paging features at all.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace dpant
{

    /// <summary>
    /// Summary description for CustomersDataLayer
    /// CustomersDataLayer is a Data Layer implementation for the Customers table of the Northwind database.
    /// Public methods:
    ///     GetCustomers     : Returns all rows from the Customers table.
    ///     GetCustomer      : Returns only one row from the Customers table.
    ///     GetCustomersCount: Returns the total number of Customers rows.
    ///     
    /// Written by dpant@yahoo.com, 13/10/2007.
    /// </summary>
    public class CustomersDataLayer
    {
        public static DataTable GetCustomers()
        {
            // Return all rows from the Customers table.
            return DataLayerHelper.Get("SELECT [CustomerID], [CompanyName], [Country] FROM [Customers]");
        }

        public static DataTable GetCustomer(int CustomerID)
        {
            // Return only one row from the Customers table.
            return DataLayerHelper.GetById("SELECT [CustomerID], [CompanyName], [Country] FROM [Customers] WHERE [CustomerID] = @CustomerID", "CustomerID", CustomerID.ToString());
        }

        public static int GetCustomersCount()
        {
            return (int)DataLayerHelper.ExecuteScalar("SELECT COUNT(CustomerID) FROM Customers");
        }

        public static DataTable GetPageCustomers(int startIndex, int maxPageRows)
        {
            // Delete the unneccessary rows and return the datatable.
            DataTable tbl= GetCustomers();
            int maxRows = GetCustomersCount();
            int max = startIndex + (maxPageRows - 1);
            if (max > maxRows)
                max = maxRows;
            for (int i = 0; i < startIndex; i++)
                tbl.Rows[i].Delete();
            for (int i = max + 1; i < maxRows; i++)
                tbl.Rows[i].Delete();
            return tbl;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions