GridView paging in C#

Afshar Mohebi picture Afshar Mohebi · Sep 23, 2010 · Viewed 7.2k times · Source

I am using PageIndexChanging event for handling GridView paging in C#. But don't know how can to use PageSize/PageNumber/PageCount there. In other word my code is forced to return all data always. Note following code:

protected void grdList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
        grdList.PageIndex = e.NewPageIndex;
        grdList.DataSource = MyGetData();
        grdList.DataBind();
}

Now how can I use real paging in this code?

Notice that MyGetData has an overload that get PageIndex and PageSize too.

UPDATE

I have set PageSize and enabled AllowPaging too. I know if I use declarative data binding I should supply GridView with count of all data. Question is how can can use count in this method.

UPDATE 2 It seems that such a thing I need is not possible, refer to Problem with Efficient Gridview paging without datasource control

Answer

Afshar Mohebi picture Afshar Mohebi · Sep 24, 2010

Efficient paging in GridView needs count of data, otherwise GridView loads all data in each page. As there is no way to tell GridView what is the count of data when not using DataSource controls, it's impossible to have efficient paging in GridView without having DataSource control. For more info go to this link and this link.