To page through data in a control that implements the IPageableItemContainer interface, DataPager control can be used. GridView has its own paging and does not implement IPageableItemContainer interface. ListView is the only control that works with DataPager.
The DataPager control supports built-in paging user interface (UI). NumericPagerField object enables users to select a page of data by page number. NextPreviousPagerField object enables users to move through pages of data one page at a time, or to jump to the first or last page of data. The size of the pages of data is set by using the PageSize property of the DataPager control. One or more pager field objects can be used in a single DataPager control. Custom paging UI can be created by using the TemplatePagerField object. In the TemplatePagerField template, the DataPager control is referenced by using the Container property which provides access to the properties of the DataPager control. These properties include the starting row index, the page size, and the total number of rows currently bound to the control.
So let's begin extending the GridView with the IPageableItemContainer interface.
Define a new class as below
/// <summary>
/// DataPagerGridView is a custom control that implements GrieView and IPageableItemContainer
/// </summary>
public class DataPagerGridView : GridView, IPageableItemContainer
{
}
MaximumRows will be equal to the PageSize property
/// <summary>
/// IPageableItemContainer's MaximumRows = PageSize property
/// </summary>
int IPageableItemContainer.MaximumRows
{
get { return this.PageSize; }
}
StartRowIndex can be calculated from the PageSize and PageIndex properties
/// <summary>
/// IPageableItemContainer's StartRowIndex = PageSize * PageIndex properties
/// </summary>
int IPageableItemContainer.StartRowIndex
{
get { return this.PageSize * this.PageIndex; }
}
The SetPageProperties method is called by the DataPager control every time that the control must update the page-related properties. When implemented by a class, the SetPageProperties method must internally update the MaximumRows and StartRowIndex properties by using the values of the maximumRows and startRowIndex parameters. So set the Grid with appropriate parameters and bind to right chunk of data.
/// <summary>
/// Set the control with appropriate parameters and bind to right chunk of data.
/// </summary>
/// <param name="startRowIndex"></param>
/// <param name="maximumRows"></param>
/// <param name="databind"></param>
void IPageableItemContainer.SetPageProperties(int startRowIndex, int maximumRows, bool databind)
{
int newPageIndex = (startRowIndex / maximumRows);
this.PageSize = maximumRows;
if (this.PageIndex != newPageIndex)
{
bool isCanceled = false;
if (databind)
{
// create the event arguments and raise the event
GridViewPageEventArgs args = new GridViewPageEventArgs(newPageIndex);
this.OnPageIndexChanging(args);
isCanceled = args.Cancel;
newPageIndex = args.NewPageIndex;
}
// if the event wasn't cancelled change the paging values
if (!isCanceled)
{
this.PageIndex = newPageIndex;
if (databind)
this.OnPageIndexChanged(EventArgs.Empty);
}
if (databind)
this.RequiresDataBinding = true;
}
}
For the DataPager to render the correct number of page buttons and enable/disable them, it needs to know the total number of rows and the Page Size. But this information is with the GridView and not known to the DataPager.As the GridView control inherits from CompositeDataboundControl which contains a variant of the CreateChildControls method that also takes in a property indicating whether the control is being bound to data or simply re-rendered. The GridView uses this method to bind to its data source and here we can place a trigger for the TotalRowCountAvailable event to be raised. Call base control's CreateChildControls method and determine the number of rows in the source then fire off the event with the derived data and then we return the original result.
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
int rows = base.CreateChildControls(dataSource, dataBinding);
// if the paging feature is enabled, determine the total number of rows in the datasource
if (this.AllowPaging)
{
// if we are databinding, use the number of rows that were created, otherwise cast the datasource to an Collection and use that as the count
int totalRowCount = dataBinding ? rows : ((ICollection)dataSource).Count;
// raise the row count available event
IPageableItemContainer pageableItemContainer = this as IPageableItemContainer;
this.OnTotalRowCountAvailable(new PageEventArgs(pageableItemContainer.StartRowIndex,pageableItemContainer.MaximumRows,totalRowCount));
// make sure the top and bottom pager rows are not visible
if (this.TopPagerRow != null)
this.TopPagerRow.Visible = false;
if (this.BottomPagerRow != null)
this.BottomPagerRow.Visible = false;
}
return rows
}


Andrey GapanukPosted Nov 29, 2014, 9:43 AM
Thank you! WORK WELL.
hamidPosted Jul 19, 2012, 5:10 PM
this project is very useful , but I want use it for vb.net . can you help me ? ( Excuse me , I am Iranian and I do not know English very well . )
Sun ThomasPosted Sep 15, 2011, 2:09 AM
When I try to go to the next page, it doesn't show any data. page 1 is fine. Should I write another bind Thanks Sun
pinkesh patelPosted Sep 23, 2010, 5:01 AM
i have try to make same web control for me but i can not add IPageableItemContainer in my .cs file please give me reply thanks
Jaime AvilaPosted Jul 28, 2010, 2:37 AM
Excelent article. Congratulations and thanx
ravi gargPosted Mar 9, 2010, 9:11 AM
tt
Lisseth GoodyearPosted Apr 8, 2009, 10:35 AM
I have a problem in this line: GridViewPageEventArgs args = new GridViewPageEventArgs(newPageIndex); this.OnPageIndexChanging(args); is the same problem that Ibrahim : An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: The GridView 'DataPagerGridView1' fired event PageIndexChanging which wasn't handled. " Please help me.
fedrok fedrixPosted Dec 19, 2008, 3:48 AM
Hi. I'm sorry but I'm still newbie in .NET. So I can't understand why use a dataPager with gridview. What does dataPager offer more than built-in paging in gridview? Thx!
Ibrahim AbbasPosted Dec 1, 2008, 6:49 PM
Hello, I added your work to my project but I have an error at this point GridViewPageEventArgs args = new GridViewPageEventArgs(newPageIndex); this.OnPageIndexChanging(args); " An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: The GridView 'DataPagerGridView1' fired event PageIndexChanging which wasn't handled. "
kojikabutosv SVPosted Aug 25, 2008, 3:49 PM
Hi, good article, only i have a questions, the MCN Controls are of the thirds. Regards