简体   繁体   English

C#中的GridView分页

[英]GridView paging in C#

I am using PageIndexChanging event for handling GridView paging in C#. 我正在使用PageIndexChanging事件来处理C#中的GridView分页。 But don't know how can to use PageSize/PageNumber/PageCount there. 但是不知道该如何使用PageSize / PageNumber / PageCount。 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. 请注意, MyGetData有得到过载PageIndexPageSize了。

UPDATE 更新

I have set PageSize and enabled AllowPaging too. 我已经设置PageSize并启用AllowPaging了。 I know if I use declarative data binding I should supply GridView with count of all data. 我知道如果使用声明性数据绑定,则应为GridView提供所有数据的计数。 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 更新2似乎我需要的东西是不可能的,请参阅没有数据源控制的有效Gridview分页问题

Efficient paging in GridView needs count of data, otherwise GridView loads all data in each page. GridView中的有效分页需要统计数据,否则GridView会在每个页面中加载所有数据。 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. 由于无法在不使用DataSource控件的情况下告诉GridView数据的数量,因此如果没有DataSource控件就无法在GridView中进行有效的分页。 For more info go to this link and this link . 有关更多信息,请转到此链接此链接

您可以在GridView控件中设置PageSize

you need to set PageSize="10" 您需要设置PageSize="10"

see in this link: http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx 请参阅此链接: 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: 如果您的MyGetData方法已经接受pageindex和pagesize,那么您所需要做的就是:

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. 但这似乎太简单了,所以我可能在这里错过了一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM