C# – ASP.NET GridView Paging using Linq query as datasource

asp.netcgridviewlinq-to-sqlpaging

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?

Related Topic