开发者

GridView paging in C#

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开发者_如何学Go 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


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.


You can set the PageSize in the GridView control.


you need to set PageSize="10"

see in this link: http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx


If your MyGetData method already accepts pageindex and pagesize, then all you'd need is:

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

but this seems a bit too simplistic so I'm probably missing something here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜