Click here to Skip to main content
Click here to Skip to main content

Tagged as

Another Paging Gridview

, 25 Jun 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Stephen Inglish
Software Developer (Senior) Harland Financial Solutions
United States United States
No Biography provided

Comments and Discussions

 
QuestionIn UpdatePanel? PinmemberKristin@NS13-Oct-09 18:05 
This is a great post. I used your approach in my parent UserControl that contains a gridview and wires up my application's common grid control behaviors.
 
But I'm tripping on one thing: if I next my GridView in an UpdatePanel, the viewstate of my UserControl is lost. When the user clicks to page the gridView, Page_Load is called (IsPostBack is false) and my ViewState is gone. If I take the gridView out of the UpdatePanel, it works fine (no Page_Load before the page event and my ViewState remains intact). I'm wondering if it has something to do with the ObjectDataSource.
 
Does your control work well in an UpdatePanel?
 
Thanks again for a helpful post!
 
Kristin

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 25 Jun 2009
Article Copyright 2009 by Stephen Inglish
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid