首页 > 代码库 > 自定义分页控件PageList

自定义分页控件PageList

 1 using System;
 2 namespace Commons.PageList
 3 {
 4     interface IPageList
 5     {
 6         int PageIndex { get; }
 7         int PageSize { get; }
 8         int TotalCount { get; }
 9         int TotalPage { get; }
10     }
11 }

 

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 
 5 namespace Commons.PageList
 6 {
 7     public class PageList<T> : IEnumerable, IPageList
 8     {
 9         private int pageIndex;
10         private int pageSize;
11         private int totalPage;
12         private int totalCount;
13 
14         private List<T> list;
15 
16         /// <summary>
17         /// 单页数据,总条数,计算出总页数,然后封装分页对象
18         /// </summary>
19         /// <param name="list"></param>
20         /// <param name="pageIndex"></param>
21         /// <param name="pageSize"></param>
22         /// <param name="totalCount"></param>
23         public PageList(List<T> list, int pageIndex, int pageSize, int totalCount)
24         {
25             this.pageIndex = pageIndex;
26             this.pageSize = pageSize;
27             this.totalCount = totalCount;
28             this.totalPage = totalCount / pageSize;
29             if (totalCount % pageSize > 0) this.totalPage += 1;
30 
31             this.list = list;
32         }
33         /// <summary>
34         /// 全部数据,筛选出当页数据且计算出总条数和总页数,然后封装成分页对象
35         /// </summary>
36         /// <param name="list"></param>
37         /// <param name="pageIndex"></param>
38         /// <param name="pageSize"></param>
39         public PageList(List<T> list, int pageIndex, int pageSize)
40         {
41             this.pageIndex = pageIndex;
42             this.pageSize = pageSize;
43             this.totalCount = list.Count;
44             this.totalPage = list.Count / pageSize;
45             if (list.Count % pageSize > 0) this.totalPage += 1;
46 
47             this.list = list.Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
48         }
49         public int PageIndex { get { return this.pageIndex; } }
50         public int PageSize { get { return this.pageSize; } }
51         public int TotalPage { get { return this.totalPage; } }
52         public int TotalCount { get { return this.totalCount; } }
53 
54         public IEnumerator GetEnumerator()
55         {
56             foreach (var item in this.list)
57             {
58                 yield return item;
59             }
60         }
61 
62     }
63 }

 

自定义分页控件PageList