分页通用类

/// <summary>
    /// 分页通用
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="pageIndex">当前页数</param>
    /// <param name="pageSize">每页多少条</param>
    /// <returns></returns>
    public static DataTable GetPagedTable(DataTable dt, int pageIndex, int pageSize)
    {
        DataTable newdt = dt.Clone();
        int rowbegin = 0;
        int rowend = 0;
        if (pageIndex <= 0)
        {
            rowbegin = 0;
            rowend = (pageIndex + 1) * pageSize;
        }
        else
        {
            rowbegin = (pageIndex - 1) * pageSize;
            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 dr = dt.Rows[i];
            newdt.ImportRow(dr);
        }
        return newdt;
    }
posted @ 2011-06-16 15:00  红鲤鱼与驴与绿鲤鱼  阅读(218)  评论(0编辑  收藏  举报