Helped by this post I created my own custom GridView control that allowed me to override the initialization of the pager and set the record count.
public class CustomGridView : GridView
{
private const string _virtualItemCount = "virtualItemCount";
private const string _currentPageIndex = "currentPageIndex";
[Browsable(true), Category("Custom")]
[Description("Set the virtual item count for this grid")]
public int VirtualItemCount {
get
{
if (ViewState[_virtualItemCount] == null)
ViewState[_virtualItemCount] = -1;
return Convert.ToInt32(ViewState[_virtualItemCount]);
}
set
{
ViewState[_virtualItemCount] = value;
}
}
private int CurrentPageIndex
{
get
{
if (ViewState[_currentPageIndex] == null)
ViewState[_currentPageIndex] = 0;
return Convert.ToInt32(ViewState[_currentPageIndex]);
}
set
{
ViewState[_currentPageIndex] = value;
}
}
public override object DataSource
{
get
{
return base.DataSource;
}
set
{
base.DataSource = value;
this.CurrentPageIndex = this.PageIndex;
}
}
protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
{
if (CustomPaging)
{
pagedDataSource.AllowCustomPaging = true;
pagedDataSource.VirtualCount = this.VirtualItemCount;
pagedDataSource.CurrentPageIndex = this.CurrentPageIndex;
}
base.InitializePager(row, columnSpan, pagedDataSource);
}
public bool CustomPaging {
get { return (this.VirtualItemCount != -1); }
}
}
Then on the CustomGridView.PageIndexChanging event we set the page index and reload our data (passing the page index and page size to our repository method):
protected void gvAccounts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvAccounts.PageIndex = e.NewPageIndex;
BindGrid(e.NewPageIndex, gvAccounts.PageSize);
}
protected void BindGrid(int pageIndex, int pageSize)
{
var accounts = AccountManager.GetAllAccounts<int>(pageIndex, pageSize, x=> x.AccountId);
gvAccounts.VirtualItemCount = accounts.TotalCount;
gvAccounts.DataSource = accounts;
gvAccounts.DataBind();
}
Hope this helps.