To answer more of @adam0101's response, what I think he means by "...It turns out my custom datasource was returning zero for the record count.", is that you may need to "re-attach" the datasource to the gridview. ASP.net automatically knows you're retriving record-'n', where 'n' is the next first row of the next page. This solution is more for cases where you don't want to allow asp.net to automatically handle data in your gridview, perhaps due to wanting to load data after another event (a LOAD DATA button for example), and not when the page has loaded for the first time. But as Adam mentioned below the real reason was he "...had created a datasource subclass that inherited ObjectDataSource, but it was implemented incorrectly.". Sorry for my assumption there Adam. Thanks
However, the rest of my solution I think will work for those stuck on using manually derived data sources.
ie
Set your GridView as per above, and note - without a DataSource property! Here is my example:
<asp:GridView ID="gvStudents" DataKeyNames="StudentID" runat="server"
ShowFooter="True" AutoGenerateColumns="False" Width="100%" AllowSorting="True" AllowPaging="true" PageSize="10" OnPageIndexChanging="gvStudents_PageIndexChanging">
Next make a private method or routine that you can call upon when ever you need to force the gridview onto a new page.
private void BindGridViewServer(GridView gv1)
{
gv1.DataSource = sdsStudents; //re-attach the datasource
gv1.DataBind(); //get a page of data AllowPaging must be true
}
Next, create the OnPageIndexChanging
method:
protected void gvStudents_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView myGV = (GridView)sender;
myGV.PageIndex = e.NewPageIndex;
BindGridViewServer(myGV);
}
For the sake of completing this answer, here is my code where I load the data when I want - not when a PostPack is fired, which is the default...
protected void btnSEARCH(object sender, EventArgs e)
{
//some code
//bind the gridview to the datasource here and then bind!
gvStudents.DataSource = sdsStudents;
gvStudents.DataBind();
//more code etc
}