1 /// <summary> 2 /// DataTable分页 3 /// </summary> 4 /// <param name="dt">DataTable</param> 5 /// <param name="PageIndex">页索引,注意:从1开始</param> 6 /// <param name="PageSize">每页大小</param> 7 /// <returns>分好页的DataTable数据</returns> 第1页 每页10条 8 public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize) { 9 if (PageIndex == 0) { return dt; } 10 DataTable newdt = dt.Copy(); 11 newdt.Clear(); 12 int rowbegin = (PageIndex - 1) * PageSize; 13 int rowend = PageIndex * PageSize; 14 15 if (rowbegin >= dt.Rows.Count) { return newdt; } 16 17 if (rowend > dt.Rows.Count) { rowend = dt.Rows.Count; } 18 for (int i = rowbegin; i <= rowend - 1; i++) { 19 DataRow newdr = newdt.NewRow(); 20 DataRow dr = dt.Rows[i]; 21 foreach (DataColumn column in dt.Columns) { 22 newdr[column.ColumnName] = dr[column.ColumnName]; 23 } 24 newdt.Rows.Add(newdr); 25 } 26 return newdt; 27 } 28 29 /// <summary> 30 /// 返回分页的页数 31 /// </summary> 32 /// <param name="count">总条数</param> 33 /// <param name="pageye">每页显示多少条</param> 34 /// <returns>如果 结尾为0:则返回1</returns> 35 public static int PageCount(int count, int pageye) { 36 int page = 0; 37 int sesepage = pageye; 38 if (count % sesepage == 0) { page = count / sesepage; } else { page = (count / sesepage) + 1; } 39 if (page == 0) { page += 1; } 40 return page; 41 }