Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Paging GridView with ROW_NUMBER()

, 5 Feb 2006
This article explains how to implement custom paging in the GridView web control using the ROW_NUMBER() function of SQL Server 2005
PagingGridView_src.zip
Images
first.gif
GoToPage.gif
last.gif
next.gif
PageSize.gif
previous.gif
Refresh.gif
Search.gif
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PagingGridView : System.Web.UI.Page
{

    public int PageNum
    {
        get { return Convert.ToInt16(ViewState["PageNum"]); }
        set { ViewState["PageNum"] = value; }
    }

    public int PageSize
    {
        get { return Convert.ToInt16(ViewState["PageSize"]); }
        set { ViewState["PageSize"] = value; }
    }

    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PageNum = 1;
            PageSize = 3;
            BindGridView();
        }

    }

    private string ConnectionString
    {
        get { return @"Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True"; }
    }

    private void BindGridView()
    {

        DataSet objDs = new DataSet();
        System.Data.SqlClient.SqlConnection
        myConnection = new SqlConnection(ConnectionString);
        System.Data.SqlClient.SqlDataAdapter myCommand;
        myCommand = new System.Data.SqlClient.SqlDataAdapter("sp_GridView_RowNumber", myConnection);
        myCommand.SelectCommand.CommandType = CommandType.StoredProcedure;

        // PageNum
        myCommand.SelectCommand.Parameters.Add(new SqlParameter("@PageNum", SqlDbType.Int, 4));
        myCommand.SelectCommand.Parameters["@PageNum"].Value = PageNum;

        // PageSize
        myCommand.SelectCommand.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int, 4));
        myCommand.SelectCommand.Parameters["@PageSize"].Value = PageSize;

        // TotalRowsNum
        SqlParameter TotalRowsNum = new SqlParameter("@TotalRowsNum", SqlDbType.Int);
        TotalRowsNum.Value = null;
        TotalRowsNum.Direction = ParameterDirection.Output;
        myCommand.SelectCommand.Parameters.Add(TotalRowsNum);
        myCommand.SelectCommand.Parameters["@TotalRowsNum"].Value = null;

        myConnection.Open();
        myCommand.Fill(objDs);

        GridView1.DataSource = objDs;
        GridView1.DataBind();

        Navigation(Convert.ToInt32(TotalRowsNum.Value.ToString()));

    }

    private void Navigation(int totalRecords)
    {
        Double totalPages = Math.Ceiling(((double)totalRecords / PageSize));

        if ((totalRecords == 1) || (totalPages == 0))
        {
            totalPages = 1;
        }

        if (PageSize > totalRecords)
        {
            PageSize = (int)totalPages;
        }

        GoToPageTxt.Text = PageNum.ToString();
        CurrentPage.Text = PageNum.ToString();
        TotalPages.Text =  totalPages.ToString();
    }


    protected void NavigationLink_Click(Object sender, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "First":
                PageNum = 1;
                break;
            case "Last":
                PageNum = Convert.ToInt16(TotalPages.Text);
                break;
            case "Next":
                PageNum = Convert.ToInt16(CurrentPage.Text) + 1;
                break;
            case "Prev":
                PageNum = Convert.ToInt16(CurrentPage.Text) - 1;
                break;
        }
        BindGridView();
    }

    protected void PageSizeImb_Click(object sender, ImageClickEventArgs e)
    {
        PageNum = 1;
        PageSize = Convert.ToInt16(PageSizeTxt.Text);
        BindGridView();
    }


    protected void GoToPageImb_Click(object sender, ImageClickEventArgs e)
    {
        if (GoToPageTxt.Text != "")
        {
            int maxPage  = Convert.ToInt32(TotalPages.Text);
            int goToPage = Convert.ToInt32(GoToPageTxt.Text);

            if (goToPage <= maxPage)
            {
                PageNum = goToPage;
                BindGridView();
            }
        }
    }


}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Socrate1
Web Developer
Canada Canada
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 5 Feb 2006
Article Copyright 2006 by Socrate1
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid