c# 分頁的方法

來源:互聯網
上載者:User

標籤:return   根據   query   sea   new   null   返回   etc   data   

返回的是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# 分頁的方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.