Paged GridView: GridViewFiller
The utility classes used by the paged GridView. You only directly work with GridViewFiller. It uses the ObjectAdaptor.
using System.Collections.Generic;
using System.Web.UI.WebControls;
/// <summary>
/// Fills a GridView with paged data
/// </summary>
public static class GridViewFiller
{
/// <summary>
/// Fills the specified gridview with a page of data.
/// </summary>
/// <param name="gv">The gridview.</param>
/// <param name="list">The single page of data.</param>
/// <param name="count">The total count (to work out number of pages).</param>
/// <param name="pageSize">Size of the page.</param>
public static void Fill(GridView gv, IList<object> list, int count, int pageSize)
{
//create an ObjectDateSource object programmatically
ObjectDataSource ods = new ObjectDataSource();
ods.ID = "ods" + gv.ID;
ods.EnablePaging = gv.AllowPaging;
ods.TypeName = "ObjectAdaptor"; //can be a common base class
ods.SelectMethod = "Select";
ods.SelectCountMethod = "Count";
ods.StartRowIndexParameterName = "startRowIndex";
ods.MaximumRowsParameterName = "maximumRows";
ods.EnableViewState = false;
//when creating, inject the data into the table adaptor
ods.ObjectCreating += delegate(object sender, ObjectDataSourceEventArgs e)
{ e.ObjectInstance = new ObjectAdaptor(list, count); };
gv.PageSize = pageSize;
gv.DataSource = ods;
gv.DataBind();
}
}