Click here to Skip to main content
12,633,732 members (32,428 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads

Stats

152.5K views
4.5K downloads
82 bookmarked
Posted

Paging GridView with ROW_NUMBER()

, 5 Feb 2006 Ms-RL
This article explains how to implement custom paging in the GridView web control using the ROW_NUMBER() function of SQL Server 2005
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, along with any associated source code and files, is licensed under Microsoft Reciprocal License

Share

About the Author

Socrate1
Web Developer
Canada Canada
No Biography provided

You may also be interested in...

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