Objectdatasource and Gridview : Sorting, paging, filtering

不想你离开。 提交于 2019-12-05 18:02:12

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.

FINALY! After 2 days of search, finaly found an alternative! Look this out!

http://www.unboxedsolutions.com/sean/archive/2005/12/28/818.aspx

cat_cap

This explanation is rather good and simple : GridView ObjectDataSource LINQ Paging And Sorting .

I know this is an old question, but here's how I'm handling this. Add an ObjectDataSource to my aspx page:

<asp:ObjectDataSource ID="myDataSource" runat="server"  
   SelectMethod="GetSearchResults" EnablePaging="true"
   StartRowIndexParameterName="startIndex" 
   MaximumRowsParameterName="pageSize"
   SortParameterName="sortBy" SelectCountMethod="GetSearchCount" >   
</asp:ObjectDataSource>

Note that it is missing a Type? I'm setting that in code, on my Page_Load

myDataSource.TypeName = this.GetType().AssemblyQualifiedName;

Then I'm using static methods on the same page (in the same class) to update the gridview:

public static int GetSearchCount()
{
    return _RowCount;//set elsewhere in code - this is the total number of rows for the query
}

public static DataTable GetSearchResults(string sortBy, int pageSize, int startIndex)
{
     //Code to dynamically generate SQL statements based on supplied parameters
     //then return a datatable containing only the data to be shown in the gridview
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!