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.
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:
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?
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.
The C# pagination logic is contained in a single Pager class that takes the following constructor arguments: totalItems (required) - the total number of items to be paged. currentPage (optional) - the current active page, defaults to the first page. pageSize (optional) - the number of items per page, defaults to 10.
Set the "VirtualItemCount" property of the GridView with the total number of records in the database. And bind the datasource with PageSize 10 and PageIndex value 0 as this is first page to show. After that we need to write code in the PageIndexChanging event of the GridView and bind the grid with the current page.
All you need is to keep track of the the current record index. You can store the record index in viewstate or in hidden fields run at = server. So, while you first load, set the pageindex to 0 and say you display 20 records. When the next is clicked, update the page index (pageindex++).
I strongly recommend that you go the ObjectDataSource route.
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.
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.
3) use the ObjectDataSource's property editor to connect it to your class and enable paging. You're all set!
I strongly suggest you check out The Code Project's Example of using ObjectDataSource and GridView as this is clearly the intended way to support what you want.
Good luck!
You have to set AllowCustomPaging="true". And when databinding do:
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.
Update 2: This method now works with GridView as of .Net 4.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With