简体   繁体   English

使用Linq查询作为数据源的ASP.NET GridView分页

[英]ASP.NET GridView Paging using Linq query as datasource

I am looking for a way to do paging with a GridView when i set the datasource at run time using a linq query. 当我在运行时使用linq查询设置数据源时,我正在寻找一种使用GridView进行分页的方法。 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();

I have the grid set to allow paging, but I get an error that says that the PageIndexChanging event has not been handled. 我有网格设置允许分页,但我收到一个错误,指出尚未处理PageIndexChanging事件。 I searched around and found the following: 我四处搜索,发现以下内容:

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

But that works well when you use a datatable but not with linq. 但是当你使用数据表而不是linq时,这很有效。 If I add a rebind in the event, it has to requery 7000 records which can be a little slow. 如果我在事件中添加重新绑定,它必须重新查询7000条记录,这可能有点慢。 Does anyone know how to fix the paging when using linq like this? 有没有人知道如何使用这样的linq修复分页?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM