Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paging in ListView with Entity Framework

I am doing paging in a Listview with Entity Framework and I'm stuck when passing startindex and maxrows after clicking the next/prev button

This is my code

private  List<WorkItem> Data(int startindex, int maxrows)
{            
       return (from x in ss.WorkItem
                    select x).OrderBy(p => p.WorkItemID).Skip(startindex).Take(maxrows).ToList();

}

protected void lvWorkItems_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
        this.DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        lvWorkItems.DataSource = Data(e.StartRowIndex,e.MaximumRows);

        lvWorkItems.DataBind();
}

My problem is how to pass startindex and maxrows when I click on next/Previous button. Please help

like image 215
Somashekhar Avatar asked Dec 03 '25 15:12

Somashekhar


1 Answers

Check it out please , it extension method :

 public static IQueryable<T> MyPage<T, TResult>(this IQueryable<T> obj, int    
 page,     int  pageSize, System.Linq.Expressions.Expression<Func<T, TResult>>  
 keySelector, bool asc, out int rowsCount)
 {
    rowsCount = obj.Count();
   if (asc)
    {
       return obj.OrderBy(keySelector).Skip(page * pageSize).Take(pageSize);
    }
   else
   {
       return obj.OrderByDescending(keySelector).Skip(page * pageSize).Take(pageSize);
   }        
}
like image 76
Ali Sarshogh Avatar answered Dec 07 '25 12:12

Ali Sarshogh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!