C# 当 DataSource 未返回完整结果集时手动设置 GridView 的 PageCount?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/566981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Manually setting a GridView's PageCount when DataSource doesn't return full result set?
提问by Dan Herbert
I'm trying to figure out ASP.NET's GridView
pagination mechanics so I can use the framework's native functionality instead of my company's home-brewed manual pagination routines which take a lot of work to implement.
我试图弄清楚 ASP.NET 的GridView
分页机制,以便我可以使用框架的本机功能,而不是我公司自制的手动分页例程,这需要大量工作才能实现。
I've figured out everything except how get the GridView
's PageCount property to work with our web services. Currently, our web services return the total record count like the following:
除了如何让GridView
的 PageCount 属性与我们的 Web 服务一起工作之外,我已经弄清楚了一切。目前,我们的网络服务返回总记录数,如下所示:
public object[] GetStuffMethod(int pageNum, int recordsPerPage, out int totalRecords)
This works fine with a GridView, however the documentation I've found says that the GrideView
's PageCount
property is generated from the total records in the DataSource. Is there really no way to set the PageCount based on something else other than returning all of the records?
这适用于 GridView,但是我发现的文档说GrideView
'sPageCount
属性是从数据源中的总记录生成的。除了返回所有记录之外,真的没有办法根据其他东西设置 PageCount 吗?
There could be tens of thousands of records in my data source so I'd rather not select all of them just to make the GridView's page count work. I probably could just ignore the GridView's page count and calculate it on my own, but if the framework has a way to do this, I'd rather use it.
我的数据源中可能有数以万计的记录,所以我宁愿不选择所有记录只是为了使 GridView 的页面计数工作。我可能可以忽略 GridView 的页数并自行计算,但如果框架有办法做到这一点,我宁愿使用它。
采纳答案by Michael La Voie
I strongly recommend that you go the ObjectDataSource route.
我强烈建议您使用 ObjectDataSource 路线。
If you are unfamiliar with this approach here are the basics:
如果您不熟悉这种方法,这里是基础知识:
1) Instead of manually setting the grid.DataSource property in the code behind, you add an extra element to the page . You set the DataSourceID of the grid to the id of your ObjectDataSource.
1) 不是在后面的代码中手动设置 grid.DataSource 属性,而是向页面添加一个额外的元素。您将网格的 DataSourceID 设置为 ObjectDataSource 的 ID。
2) This is where you get real control. You create a new class and give it two functions "SelectRows()" and "GetCount()". You can put your logic in both functions and optimize to your heart's content. Feel free to use web services if that's what you need to work with, but under this method, you can call one to return rows and other to return the count.
2)这是您获得真正控制权的地方。您创建一个新类并为其提供两个函数“SelectRows()”和“GetCount()”。您可以将您的逻辑放在这两个功能中,并根据您的内心需求进行优化。如果您需要使用 Web 服务,请随意使用 Web 服务,但在此方法下,您可以调用一个来返回行,另一个来返回计数。
3) use the ObjectDataSource's property editor to connect it to your class and enable paging. You're all set!
3) 使用 ObjectDataSource 的属性编辑器将其连接到您的类并启用分页。你都准备好了!
I strongly suggest you check out The Code Project's Example of using ObjectDataSource and GridViewas this is clearly the intended way to support what you want.
我强烈建议您查看代码项目的使用 ObjectDataSource 和 GridView 的示例,因为这显然是支持您想要的东西的预期方式。
Good luck!
祝你好运!
回答by eglasius
You have to set AllowCustomPaging="true". And when databinding do:
您必须设置 AllowCustomPaging="true"。当数据绑定时:
mygrid.VirtualItemCount = totalRecords;
mygrid.DataSource = mysource;
mygrid.DataBind();
Update 1:Above is for datagrid only. Unfortunately the only supported way to do server side paging with the gridview is implementing an object datasource or implementing a custom datasource. Here is the related msdn doc http://msdn.microsoft.com/en-us/library/5aw1xfh3.aspx.
更新 1:以上仅适用于数据网格。不幸的是,使用 gridview 进行服务器端分页的唯一支持方法是实现对象数据源或实现自定义数据源。这是相关的 msdn 文档http://msdn.microsoft.com/en-us/library/5aw1xfh3.aspx。
Update 2:This method now works with GridView as of .Net 4.5
更新 2:从 .Net 4.5 开始,此方法现在适用于 GridView