加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

对DataTable进行分页

发布时间:2020-12-16 23:03:48 所属栏目:大数据 来源:网络整理
导读:在某些情况下可能需要必须对datatable进行分页如下: C#: /**/ /// summary /// 对DataTable进行分页,起始页为1 /// /summary /// param name="dt"/param /// param name="PageIndex"/param /// param name="PageSize"/param /// returns/returns public st

在某些情况下可能需要必须对datatable进行分页如下:

C#:

/**/ /// <summary>
/// 对DataTable进行分页,起始页为1
/// </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)
... {
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;
}

VB.net:

Public Shared Function GetPagedTable(ByVal dt As DataTable,ByVal PageIndex As Integer,ByVal PageSize As Integer) As DataTable If PageIndex = 0 Then Return dt End If Dim newdt As DataTable = dt.Copy() newdt.Clear() Dim rowbegin As Integer = (PageIndex - 1) * PageSize Dim rowend As Integer = PageIndex * PageSize If rowbegin >= dt.Rows.Count Then Return newdt End If If rowend > dt.Rows.Count Then rowend = dt.Rows.Count End If For i As Integer = rowbegin To rowend - 1 Dim newdr As DataRow = newdt.NewRow() Dim dr As DataRow = dt.Rows(i) For Each column As DataColumn In dt.Columns newdr(column.ColumnName) = dr(column.ColumnName) Next newdt.Rows.Add(newdr) Next Return newdt End Function

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读