码迷,mamicode.com
首页 > 其他好文 > 详细

将DataTable进行分页并生成新的DataTable

时间:2014-09-24 19:49:47      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   div   sp   c   log   

        /// <summary>
        /// 将DataTable进行分页并生成新的DataTable  
        /// </summary>
        /// <param name="dt">原始Datatable</param>
        /// <param name="PageIndex">需要的第n页</param>
        /// <param name="PageSize">每页页数</param>
        /// <returns></returns>
        public DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize, out int recound)
        {
            if (PageIndex < 1)
            {
                PageIndex = 1;
            }
            else if (PageIndex > 1 || (PageIndex == 1))
            {
                PageIndex = PageIndex + 1;
            }
            DataTable newdt = dt.Copy();
            newdt.Clear();
            recound = dt.Rows.Count;
            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;
            }
            //生成新的DataTable
            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;
        }

 

将DataTable进行分页并生成新的DataTable

标签:style   blog   color   ar   for   div   sp   c   log   

原文地址:http://www.cnblogs.com/dragon-L/p/3991100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!