Click here to Skip to main content
15,881,898 members
Articles / Programming Languages / C#

Another Paging Gridview

Rate me:
Please Sign up or sign in to vote.
4.56/5 (5 votes)
25 Jun 2009CPOL1 min read 17.3K   9   1
Another Paging Gridview

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:

C#
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.

C#
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();
} 
C#
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)


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

Comments and Discussions

 
QuestionIn UpdatePanel? Pin
Kristin@NS13-Oct-09 18:05
Kristin@NS13-Oct-09 18:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.