C# 中的 GridView 分页

发布于 2024-09-24 18:58:32 字数 832 浏览 0 评论 0 原文

我正在使用 PageIndexChanging 事件来处理 C# 中的 GridView 分页。但不知道如何在那里使用 PageSize/PageNumber/PageCount 。换句话说,我的代码被迫始终返回所有数据。请注意以下代码:

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

现在我如何在此代码中使用真正的分页?

请注意,MyGetData 也有一个重载,可以获取 PageIndexPageSize

更新

我也设置了PageSize并启用了AllowPaging。我知道如果我使用声明性数据绑定,我应该向 GridView 提供所有数据的计数。问题是如何在此方法中使用计数。

更新2 看来我需要的这样的事情是不可能的,请参阅 问题无需数据源控制的高效Gridview分页

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恬淡成诗 2024-10-01 18:58:32

GridView 中的高效分页需要对数据进行计数,否则 GridView 会加载每个页面中的所有数据。由于在不使用 DataSource 控件时无法告诉 GridView 数据的数量是多少,因此如果没有 DataSource 控件,则不可能在 GridView 中进行有效的分页。有关更多信息,请访问此链接此链接

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.

落花随流水 2024-10-01 18:58:32

您可以设置页面大小GridView 控件中。

You can set the PageSize in the GridView control.

眼趣 2024-10-01 18:58:32

您需要设置 PageSize="10"

请参阅此链接:http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx

you need to set PageSize="10"

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

蒲公英的约定 2024-10-01 18:58:32

如果您的 MyGetData 方法已经接受 pageindex 和 pagesize,那么您所需要的只是:

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

但这似乎有点过于简单,所以我可能在这里遗漏了一些东西。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文