C#-DAtaTable 페이지 나누기 코드

5727 단어
        /// <summary>
        /// DataTable            
        /// </summary>
        /// <param name="dtAll">DataTable</param>
        /// <param name="pageNo">  ,  : 1  </param>
        /// <param name="pageSize">    </param>
        /// <returns>     DataTable  </returns>
        private DataTable getOnePageTable(DataTable dtAll, int pageNo, int pageSize)
        {
            var totalCount = dtAll.Rows.Count;
            var totalPage = getTotalPage(totalCount, pageSize);
            var currentPage = pageNo;
            currentPage = (currentPage > totalPage ? totalPage : currentPage);//  PageNo  ,   PageNo=PageCount
            currentPage = (currentPage <= 0 ? 1 : currentPage);//  PageNo<=0,     
            //----        
            var onePageTable = dtAll.Clone();
            //----  1      
            var rowBegin = (currentPage - 1) * pageSize;
            var rowEnd = currentPage * pageSize;
            rowEnd = (rowEnd > totalCount ? totalCount : rowEnd);
            for (var i = rowBegin; i <= rowEnd - 1; i++)
            {
                var newRow = onePageTable.NewRow();
                var oldRow = dtAll.Rows[i];
                foreach (DataColumn column in dtAll.Columns)
                {
                    newRow[column.ColumnName] = oldRow[column.ColumnName];
                }
                onePageTable.Rows.Add(newRow);
            }
            return onePageTable;
        }

        /// <summary>
        ///          
        /// </summary>
        /// <param name="totalCount">     </param>
        /// <param name="pageSize">      </param>
        /// <returns>   </returns>
        public int getTotalPage(int totalCount, int pageSize)
        {
            var totalPage = (totalCount / pageSize) + (totalCount % pageSize > 0 ? 1 : 0);
            return totalPage;
        }

좋은 웹페이지 즐겨찾기