首页 > 代码库 > c# 分页的方法

c# 分页的方法

返回的是list集合:

/// <summary>
        ///  返回合同的款项信息
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="currentpage"></param>
        /// <param name="totalcount"></param>
        /// <returns></returns>
        public List<Contract.Model.MoneyLog> GetContractMoneyLogs(int pagesize, int currentpage, out int totalcount)
        {
            try
            {
                totalcount = 0;
                if (this.ContractID == 0) throw new ContractException("合同编号不存在");
                List<Contract.Model.MoneyLog> listm = CClient.GetContractMoneyLogList(this.ContractID);
                if (listm != null)
                {
                    totalcount = listm.Count;
                    //记录日志
                    string msg = string.Format("查询合同款项信息 记录数:{0}", listm.Count);
                    Model.ContractLog L = new ContractLog(this.ContractID, msg, this.UserID);
                    CClient.AddContractLogToDB(L);
                    //对返回的listm集合进行分页操作
                    return ((List<Contract.Model.MoneyLog>)FilterList(pagesize, currentpage, listm));
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                CClient.Close();
            }
        }
/// <summary>
        /// 根据总条数和当前页,返回当前页的list
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="currentpage"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<T> FilterList<T>(int pagesize, int currentpage, List<T> list)
        {
            try
            {
                int totalcount = list.Count;
                if (totalcount <= pagesize)
                    return list;

                int totalpage = totalcount / pagesize + ((totalcount % pagesize == 0) ? 0 : 1);
                int querysize = pagesize;
                //如果是最后一页,需要计算一下最后剩余的记录条数
                if (currentpage >= totalpage) querysize = (totalcount % pagesize == 0) ? pagesize : totalcount % pagesize;
                return list.GetRange((currentpage - 1) * pagesize, querysize);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 

 

返回的是DataTable

/// <summary>
        /// 获取待审批或已审批合同列表,此方法全部由王君添加
        /// </summary>
        /// <param name="userid">当前登录账号的 userid</param>
        /// <param name="auditstate">审核状态:未审 0,已审 1</param>
        /// <returns></returns>
        public DataTable SearchAuditContracts(int auditstate, int pageIndex, int pageSize, out int totalcount)
        {
            try
            {
                totalcount = 0;
                DataTable results = CClient.SearchAuditContracts(this.UserID, auditstate);
                if (results.Rows.Count > 0)
                {
                    //记录日志
                    string msg = string.Format("查询合同审批列表信息 记录数:{0}", results.Rows.Count);
                    Model.ContractLog L = new ContractLog(this.ContractID, msg, this.UserID);
                    CClient.AddContractLogToDB(L);
                    totalcount = results.Rows.Count;
                    return Utility.SplitDataTable(results, pageIndex, pageSize);
                }
                return results;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                CClient.Close();
            }
        }

 

/// <summary>
        /// 根据索引和pagesize返回记录
        /// </summary>
        /// <param name="dt">记录集 DataTable</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="pagesize">一页的记录数,此方法全部由王君添加</param>
        /// <returns></returns>
        public static DataTable SplitDataTable(DataTable dt, int PageIndex, int PageSize)
        {
            int totalcount = dt.Rows.Count;
            if (PageIndex == 0)
                return dt;
            DataTable newdt = dt.Clone();
            //newdt.Clear();
            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;

            if (rowbegin >= dt.Rows.Count)
                return newdt;

            if (rowend > dt.Rows.Count)
                rowend = dt.Rows.Count;
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }

            return newdt;
        }

 

c# 分页的方法