开发者

ASP.NET GridView Paging using Linq query as datasource

开发者 https://www.devze.com 2023-01-13 02:14 出处:网络
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:

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_i开发者_如何学Pythond,
                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. 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. 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?


A possible solution:

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

http://www.dbtutorials.com/display/linq-to-sql-paging-cs.aspx

Other possibility here:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消