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;
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
};
gvFeats.VirtualItemCount = query.Count();
gvFeats.DataSource = query.Take(gvFeats.PageSize);
gvFeats.DataBind();
}
protected void gvFeats_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Entities db = new Entities();
var query = from x in db.MyTable
orderby x.Name
select new
{
MyTableID = x.MyTableID,
Name = x.Name
};
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