ASP.NET GridView Paging using Linq query as datasource

15,639

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

Share:
15,639
ecspot
Author by

ecspot

Updated on June 04, 2022

Comments

  • ecspot
    ecspot almost 2 years

    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();
    

    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?