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

Implement Paging using ObjectDataSource with GridView

Rate me:
Please Sign up or sign in to vote.
3.51/5 (23 votes)
30 Apr 20066 min read 192.3K   3.6K   55  
Demonstration of paging and Sorting with example , using ObjectDataSource and GridView
using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// Represents Products data
/// </summary>
public class ProductsList
{
	public ProductsList()
	{
		//
		// TODO: Add constructor logic here
		//
	}
   
    public DataView GetProducts(int StartRow,int PageSize)
    {
       //this method Returns the Data from ProductsList.xml file
        
        //getdata() could be implemented to read from database as well
        return GetData(StartRow, PageSize);
    }

    private DataView GetData(int StartRow, int PageSize)
    {
        //this method reads data from xml file present in App_Code 
        DataSet Products = new DataSet();
        Products.ReadXml(HttpContext.Current.Server.MapPath("~/App_Data/ProductsList.xml"));
        
        DataTable ProductsTable= Products.Tables[0];

        //create new empty table to hold the resultant rows 
        DataTable PagedProductsTable = ProductsTable.Clone();
        //DataRow ProductsRow;

        //   i = NewPageIndex*PageSize gives us the starting row of new page
        for (int i = StartRow; i < StartRow + PageSize && i < ProductsTable.Rows.Count; i++)
        {
            //add the rows 
            PagedProductsTable.ImportRow(ProductsTable.Rows[i]);
        }
        return PagedProductsTable.DefaultView;
    }

    public int GetRowsCount()
    {
        //this method returns the total number of rows in the xml file
        DataSet Products = new DataSet();
        Products.ReadXml(HttpContext.Current.Server.MapPath("~/App_Data/ProductsList.xml"));
        return Products.Tables[0].Rows.Count;
    }
}

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


Written By
Web Developer
United States United States
I m a software developer working for about 2 years in the software industry. I have been focused primarily on .NET

I love to work develop for web using new technologies.

You will see more articles coming up about ASP.net 2.0.

Comments and Discussions