Handling the gridview’s pageindex from code

When binding a custom dataset to a gridview and enabling paging, you will be greeted by an error stating that the griview started the PageIndexChanging event, and that the event wasn’t handled.

This occurs because the built in sqldatasource will handle paging for you by caching the dataset, and by binding your own dataset you bypassed this behaviour.

If you have no further custom handling you need, it is sufficient to pass the new page index from the event to the gridview, and rebinding your own data. eg:

protected void GridViewSomeView_PageIndexChanging(object sender, GridViewPageEventArgs e) {
    GridViewSomeView.PageIndex = e.NewPageIndex;
    BindData(); // your data event
}