首页 > 代码库 > 将DataTable进行分页并生成新的DataTable
将DataTable进行分页并生成新的DataTable
/// <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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。