C#封装好的文件分页类

35 篇文章 0 订阅

先建立一个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;
            }
        }
    }
}





后来直接拿来调用就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值