C#的DataTable分页

方法一:

public static DataTable DtPage(DataTable dt, int pageIndex, int pageSize)
{
            DataTable pagedt = dt.Clone();
            var query = dt.AsEnumerable().Skip(pageIndex).Take(pageSize);
            foreach (DataRow item in query)
            {
                pagedt.Rows.Add(item.ItemArray);
 
            }

            return pagedt;
}

方法二:

public static DataTable DtPage(DataTable dt, int pageIndex, int pageSize)
{
            if (pageIndex== 0) { return dt; }

            DataTable newdt = dt.Copy();
            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;
}

 返回分页后的总页数:

 public int getTotalPage(int totalCount, int pageSize)
{
            var totalPage = (totalCount / pageSize) + (totalCount % pageSize > 0 ? 1 : 0);
            return totalPage;
}

上一篇:简易电路设计,PW1605芯片实现24V/30V/48V限流过压保护功能


下一篇:面试算法-107-二叉搜索树迭代器-题目