4

I have an ASP.NET 4 GridView control that uses the logic discussed in these 2 articles:

How does the ASP.NET 4.5 GridView.AllowCustomPaging property make this simpler?

A link to an article on how to use it would be very welcome.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Cesar Daniel
  • 707
  • 1
  • 9
  • 25

1 Answers1

5

In my recent experience, it doesn't.

Specifically: When implementing efficient GridView custom paging (retrieving only the required page of data from a very large database table) while using ASP.NET 4.5's Model Binding system, the GridView.AllowCustomPaging property didn't enter into it.

Setting the GridView.SelectMethod property causes Model Binding to be used, which gives us the "ObjectDataSource-style" functionality (as described in your links) without requiring an ObjectDataSource. Within this approach, there are two options:

(1) The method specified by GridView.SelectMethod returns an IQueryable, e.g.:

public IQueryable<MyClass> MySelectMethod1()
{
    return myService.GetAll(someCriteria);
}

If you don't mind exposing an IQueryable to your Presentation Tier, then this is a very fast way of implementing very efficient paging. At runtime, the framework automatically applies Skip() and Take() methods to your source, based on the GridView's PageIndex and PageSize properties. Only the required page of results is returned from the database. Easy!

(2) The method specified by GridView.SelectMethod returns some other bindable object, e.g.:

public IList<MyClass> MySelectMethod2(int startRowIndex, int maximumRows, out int totalRowCount)
{
    totalRowCount = myService.GetCount(someCriteria);
    return myService.GetPage(someCriteria, startRowIndex, maximumRows);
}

By setting totalRowCount, we have now given the GridView all the information it needs to correctly render its Pager, while having retrieved only the required page of data from the database.

I had expected to use the VirtualItemCount property (as described here), but as far as I can tell, the totalRowCount out-parameter obviates the VirtualItemCount property.

If (1) or (2) aren't implemented, then the GridView will throw an exception:
When the DataBoundControl has paging enabled, either the SelectMethod should return an IQueryable or should have all these mandatory parameters : int startRowIndex, int maximumRows, out int totalRowCount

So, we have implemented GridView custom paging in ASP.NET 4.5.... but with GridView.AllowCustomPaging and GridView.VirtualItemCount nowhere to be seen!

Community
  • 1
  • 1
Merenzo
  • 5,326
  • 4
  • 31
  • 46