美文网首页
C#将DataTable分页

C#将DataTable分页

作者: 易点梅 | 来源:发表于2021-07-30 10:04 被阅读0次
    /// <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;
            }
    
    

    相关文章

      网友评论

          本文标题:C#将DataTable分页

          本文链接:https://www.haomeiwen.com/subject/xapqvltx.html