ASP.NET的GridView分页使用Linq查询作为数据源 [英] ASP.NET GridView Paging using Linq query as datasource

查看:107
本文介绍了ASP.NET的GridView分页使用Linq查询作为数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个方式做分页用当我使用LINQ查询在运行时设置数据源的GridView。这里是我的code:

I am looking for a way to do paging with a GridView when i set the datasource at run time using a linq query. here is my code:

ETDataContext etdc = new ETDataContext();
var accts = from a in etdc.ACCOUNTs
            orderby  a.account_id
            select new
            {
                Account = a.account_id,
                aType = a.SERVICEs.FirstOrDefault().SERVICE_TYPE.service_type_desc,
                name = a.SERVICEs.FirstOrDefault().service_name,
                Letter_dt = a.create_dt,
                PrimAccthldr = a.PEOPLE.first_name + " " + a.PEOPLE.middle_name + " " + a.PEOPLE.last_name
             };
GridView1.DataSource = accts;
GridView1.BindData();

我有网格设置为允许分页,但我得到了说,PageIndexChanging事件尚未处理的错误。我搜索了一圈,发现以下内容:

I have the grid set to allow paging, but I get an error that says that the PageIndexChanging event has not been handled. I searched around and found the following:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
     GridView1.PageIndex = e.NewPageIndex;
     GridView1.DataBind();
}

但是,效果很好,当你使用一个DataTable但与LINQ。如果我在事件中添加一个重新绑定,它必须重新查询7000条记录它可以是一个有点慢。有谁知道如何使用LINQ像这样的时候来解决分页?

But that works well when you use a datatable but not with linq. If I add a rebind in the event, it has to requery 7000 records which can be a little slow. Does anyone know how to fix the paging when using linq like this?

推荐答案

一个可能的解决方案:

http://www.devtoolshed.com/content/gridview-objectdatasource-linq-paging-and-sorting

http://www.dbtutorials.com/display/ LINQ到SQL的分页cs.aspx

在这里的另一种可能性:

Other possibility here:

使用LINQ数据源作为由Scott Guthrie的(ASP的父亲)描述
<一href=\"http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx\" rel=\"nofollow\">http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx

Use LINQ Data Source as described by Scott Guthrie (the father of ASP) http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx

这篇关于ASP.NET的GridView分页使用Linq查询作为数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆