![]() |
Web Development »
ASP.NET Controls »
Grid Controls
License: The Code Project Open License (CPOL)
Another Paging GridviewBy Stephen InglishAnother Paging Gridview |
C# (C# 1.0, C# 2.0, C# 3.0)
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
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!
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
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 |