Objectdatasource和Gridview:排序,分页,过滤

时间:2009-08-14 17:04:41

标签: asp.net sorting paging objectdatasource

我使用实体框架1.0并尝试使用可访问我的外观的objectdatasource输出Gridview。问题是,它似乎特别困难,并没有看到任何真正做我想在互联网上做的事情。

对于那些知道,gridview需要一个objectdatasource,它不能自动排序然后你必须手动完成。没那么糟糕。它变成了一场噩梦,当我们将分页和过滤器设置添加到gridview的数据源时。

在互联网上搜索了好几个小时后,我问你们,伙计们,如果有人知道一个链接,可以解释我如何混合 Pagging 排序和<对gridview和objectdatasource进行strong>过滤!

提前致谢,对不起我的英语。

4 个答案:

答案 0 :(得分:5)

对你来说可能不再感兴趣了,但我想我发了一个答案:

我正在使用Linq2Sql和ObjectDataSource,它可以很好地进行分页和排序。

我实现了一个用作ObjectDataSource的Class。它有一个Select和Count方法,调用我的业务层使用 Linq2SQL 查询从DB中检索数据,应该与EntityFramework类似。 select方法自动获取第一个项索引,页面大小和排序表达式作为参数。

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

在ASPX中,DataSource的配置如下:

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

Select和Count方法使用Linq查询从数据库中检索数据。我使用 Skip(),Take() Orderby()方法。为了让OrderBy接受字符串排序表达式,我使用DynamicLinq没有太多代码,数据绑定,分页和排序自动工作。

如果您有兴趣,我可以发布我的代码的更多细节。

答案 1 :(得分:1)

最后!经过2天的搜索,终于找到了另类!看看这个!

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

答案 2 :(得分:1)

答案 3 :(得分:0)

我知道这是一个老问题,但这就是我如何处理这个问题。将ObjectDataSource添加到我的aspx页面:

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

请注意,它缺少一个类型?我在代码中设置了我的Page_Load

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

然后我在同一页面(在同一个类中)使用静态方法来更新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
}