先建立一个FileListPager.cs类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
namespace FileCollectionSystem.DataFetch
{
/// <summary>
/// 文件列表分页
/// </summary>
public class FileListPager<T>
{
public List<T> AllFiles
{
get;
set;
}
/// <summary>
/// 总页数
/// </summary>
public int TotalPage
{
get;
set;
}
/// <summary>
/// 当前页
/// </summary>
public int CurPage
{
get;
set;
}
/// <summary>
/// 显示记录数
/// </summary>
[DefaultValue(5)]
public int PageSize
{
get;
set;
}
public FileListPager(List<T> lstFile)
: this(lstFile, 5)
{
}
public FileListPager(List<T> lstFile, int iPageSize)
{
if (lstFile == null || lstFile.Count == 0) return;
AllFiles = lstFile;
if (iPageSize <= 0)
PageSize = 5;
else
PageSize = iPageSize;
TotalPage =
(AllFiles.Count / PageSize) + ((AllFiles.Count % PageSize == 0) ? 0 : 1);
}
/// <summary>
/// 根据页数获取文件
/// </summary>
/// <param name="iPage">页数</param>
/// <returns></returns>
public List<T> GetFileList(int iPage)
{
try
{
if (AllFiles == null || AllFiles.Count == 0)
return new List<T>();
if (iPage <= 0) iPage = 1;
else if (iPage > TotalPage) iPage = TotalPage;
int iStart = (iPage - 1) * PageSize;
int iEnd = iStart + (PageSize - 1);
List<T> lstResult = new List<T>();
for (int i = iStart; i <= iEnd; i++)
{
if (i >= 0 && i < AllFiles.Count)
lstResult.Add(AllFiles[i]);
}
CurPage = iPage;
return lstResult;
}
catch (Exception ex)
{
SysLogManagement.LogManagement.Logger.WriteError("GetFileList", ex);
return null;
}
}
}
}
后来直接拿来调用就可以了。