对DataTable进行分页

前端之家收集整理的这篇文章主要介绍了对DataTable进行分页前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

C#:

@H_301_6@ /**/ /// <summary>
/// 对DataTable进行分页,起始页为1
/// </summary>
/// <param name="dt"></param>
/// <param name="PageIndex"></param>
/// <param name="PageSize"></param>
/// <returns></returns>

@H_301_6@ public @H_301_6@ static @H_301_6@ DataTable GetPagedTable(DataTable dt, int @H_301_6@ PageIndex, int @H_301_6@ PageSize)
... @H_301_6@{
if @H_301_6@ (PageIndex @H_301_6@== @H_301_6@ @H_301_6@0 @H_301_6@)
return @H_301_6@ dt;
DataTable newdt @H_301_6@= @H_301_6@ dt.Copy();
newdt.Clear();

int @H_301_6@ rowbegin @H_301_6@= @H_301_6@ (PageIndex @H_301_6@- @H_301_6@ @H_301_6@1 @H_301_6@) @H_301_6@* @H_301_6@ PageSize;
int @H_301_6@ rowend @H_301_6@= @H_301_6@ PageIndex @H_301_6@* @H_301_6@ PageSize;

if @H_301_6@ (rowbegin @H_301_6@>= @H_301_6@ dt.Rows.Count)
return @H_301_6@ newdt;

if @H_301_6@ (rowend @H_301_6@> @H_301_6@ dt.Rows.Count)
rowend @H_301_6@= @H_301_6@ dt.Rows.Count;
for @H_301_6@ ( int @H_301_6@ i @H_301_6@= @H_301_6@ rowbegin; i @H_301_6@<= @H_301_6@ rowend @H_301_6@- @H_301_6@ @H_301_6@1 @H_301_6@; i @H_301_6@++ @H_301_6@)
... @H_301_6@{
DataRow newdr
@H_301_6@= @H_301_6@ newdt.NewRow();
DataRow dr @H_301_6@= @H_301_6@ dt.Rows[i];
foreach @H_301_6@ (DataColumn column in @H_301_6@ dt.Columns)
... @H_301_6@{
newdr[column.ColumnName]
@H_301_6@= @H_301_6@ dr[column.ColumnName];
} @H_301_6@
newdt.Rows.Add(newdr);
} @H_301_6@

return @H_301_6@ newdt;
}

VB.net:

@H_301_6@ 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

猜你在找的VB相关文章