views:

47

answers:

2
+1  Q: 

Paging in Gridview

In my gridview I have 6 pages. When I go to page two and try to sort by descending on any of my columns I get sent back to page 1. Is there a way to stay at page two?

+1  A: 

You'll need to assign the current PageIndex property of the GridView in your sorting method after the sort but before the DataBind.

David Neale
It doesn't work because its sending me back to page 1
Jeff
Are you setting the **PageIndex** of your gridview in your **sorting method** as david suggests? Did you find another solution?
senloe
A: 

In PageIndexChanging event, you need to set the page and rebind your data. For instance like following:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    bindGridView();//a method that re/sets the datasource
}

Check this article.

KMan
It doesn't work because its sending me back to page 1
Jeff
Did you turn on the Paging? `AllowPaging="True"` and `PageSize="10"`. Also on page load I believe you have something like this `if (!IsPostBack) bindGridView();`
KMan
Did you turn on the Paging? YesAllowPaging="True" Yes and PageSize="10" Yes. Also on page load I believe you have something like this if (!IsPostBack) bindGridView(); I just tried that and it didnt work.The problem is not paging and sorting. The problem is staying on page 2 for example after the sort is done??
Jeff
@Jeff: For that matter, see Tim's response. One workaround could, save the currentPageNo in `ViewState`, as soon as the Sort is done, manually set the `GridView1.PageIndex` to currentPageNo and reset datasource.
KMan