Objectdatasource and Gridview : Sorting, paging, filtering

Simon Dugré picture Simon Dugré · Aug 14, 2009 · Viewed 10.5k times · Source

Im using entity framework 1.0 and trying to feed out a Gridview with a objectdatasource that have access to my facade. The problem is, that it seems to be particulary difficult and haven't seen anything that realy do what i want it to do on the internet.

For those who know, a gridview feeded with an objectdatasource, it can't sort automaticaly then you must do it manually. It's not that bad. Where it becomes a nightmare, its when we add paging and filter settings to a gridview's datasource.

After many hours searching on the internet, i'm asking you, guys, if anyone knows a link that can explain me how to mix Pagging, Sorting and filtering for a gridview and an objectdatasource!

Thanks in advance and sorry for my english.

Answer

PeterFromCologne picture PeterFromCologne · Jun 11, 2010

It may no longer be of interest for you, but I thought I post an answer nevertheless:

I am using Linq2Sql and a ObjectDataSource and it does Paging and Sorting very well.

I implemented a Class to be used as the ObjectDataSource. It has a Select and a Count method calling my business layer which uses Linq2SQL queries to retrieve data from the DB, should be similar with the EntityFramework. The select methods gets the first item index, page size and the sort expression as parameters automatically.

public List<EntityClass> Select(int startIndex, int pageSize, string sortBy) {}
public int Count() {}

In the ASPX, the DataSource is configured like this:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"  
     SelectMethod="Select" EnablePaging="true"
     StartRowIndexParameterName="startIndex" 
     MaximumRowsParameterName="pageSize"
     SortParameterName="sortBy" SelectCountMethod="Count" >   
</asp:ObjectDataSource>

The Select and the Count method use Linq queries to retrieve the data from the DB. I use the Skip(), Take() and Orderby() methods. For the OrderBy to accept a string sort expression I use DynamicLinq There is not much to code, Databinding, paging and Sorting are automatically working.

If you are interested I could post more details of my code.