Click here to Skip to main content
6,630,289 members and growing! (22,346 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Grid Controls License: The Code Project Open License (CPOL)

Another Paging Gridview

By Stephen Inglish

Another Paging Gridview
C# (C# 1.0, C# 2.0, C# 3.0)
Version:4 (See All)
Posted:25 Jun 2009
Views:2,938
Bookmarked:6 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
1 vote for this article.
Popularity: 0.00 Rating: 4.00 out of 5

1

2

3
1 vote, 100.0%
4

5

Background 

My current deployment had a need for a control that provided simple paging, and nothing more, so after stumbling around the internet, I found this code. However, upon looking again, I couldn't find the code, so I thought I would post it here, so anyone interested could use it. If you know who the author of this code is originally, or if you are the author, please reply so I can give you full credit.

Using the Code 

Here is the source code for the PagingGridView:

namespace ServerControls
{
    public class PagingGridView : GridView
    {
        private object gvData;

        public override Object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                if (value is IList || value is DataSet || value is IQueryable)
                {
                    gvData = value;
                    ObjectDataSource ods = new ObjectDataSource();

                    ods.ID = "ods_" + this.ID;

                    ods.EnablePaging = this.AllowPaging;
                    // This must be the full name of the class
		  ods.TypeName = "ServerControls.VirtualItemCountTableAdapter"; 
                    ods.SelectMethod = "GetData";
                    ods.SelectCountMethod = "GetVirtualItemCount";
                    ods.StartRowIndexParameterName = "startRow";
                    ods.MaximumRowsParameterName = "maxRows";
                    ods.EnableViewState = false;

                    ods.ObjectCreating += new ObjectDataSourceObjectEventHandler
							(ods_ObjectCreating);

                    base.DataSource = ods;
                }
                else
                {
                    base.DataSource = value;
                }
            }
        }

        private void ods_ObjectCreating(Object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = 
		new VirtualItemCountTableAdapter(gvData, VirtualItemCount);
        }

        public Int32 VirtualItemCount
        {
            get
            {
                return (int)(ViewState["ods" + this.ID] ?? 0);
            }
            set
            {
                ViewState["ods" + this.ID] = value;
            }
        }
    }

    public class VirtualItemCountTableAdapter
    {
        private object data;
        private Int32 virtualItemCount;

        public VirtualItemCountTableAdapter(object data, Int32 virtualItemCount)
        {
            this.data = data;
            this.virtualItemCount = virtualItemCount;
        }

        public object GetData() { return data; }

        public Int32 GetVirtualItemCount() { return virtualItemCount; }

        public object GetData(int startRow, int maxRows) { return data; }
    }
}

The way this works, the PagingGridView acts like a normal gridview, except when an IList, DataSet or IQueryable is provided as the type of the datasource. When provided, it will internally create its own ObjectDataSource, preconfigured to page data based on the GridView's options.

To display a number of pages, a count of how many records this method effects should be provided to the VirtualItemCount property.  The VirtualItemCount property is a throw back to the DataGrid.

Points of Interest

This control definitely shines when you combine it with LINQ to SQL or LINQ to Entities.

protected void btnSearch_Click(object sender, EventArgs e)
{
   Entities db = new Entities();

   var query = from x in db.MyTable
            orderby x.Name
            select new
            {
               MyTableID = x.MyTableID,
               Name = x.Name
            };

   // Utilize the query to retrieve the full data count
   gvFeats.VirtualItemCount = query.Count();

   // Utilize the query to retrieve the proper page
   gvFeats.DataSource = query.Take(gvFeats.PageSize);
   gvFeats.DataBind();
} 
protected void gvFeats_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
   Entities db = new Entities();

   // Define the query including the search results 
   // (MODIFICATIONS TO THIS should also be made in the btnSearch_Click method) 
   var query = from x in db.MyTable
                   orderby x.Name
                   select new
                   {
                      MyTableID = x.MyTableID,
                      Name = x.Name                    
                   };

    // Take the next page of data from the query
    gvFeats.DataSource = query.Skip(e.NewPageIndex * 
		gvFeats.PageSize).Take(gvFeats.PageSize);
    gvFeats.PageIndex = e.NewPageIndex;
    gvFeats.DataBind();
}

See how simple it was to perform server side paging?  Skip to the proper page, then take a page size. Done!

History

  • 25th June, 2009: Initial post

License

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

About the Author

Stephen Inglish


Member

Occupation: Software Developer
Company: Sogeti USA
Location: United States United States

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralIn UpdatePanel? PinmemberKristin@NS19:05 13 Oct '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jun 2009
Editor: Deeksha Shenoy
Copyright 2009 by Stephen Inglish
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project