gpt4 book ai didi

c# - 如何将 DataPager 与数据库分页一起使用

转载 作者:太空狗 更新时间:2023-10-30 01:11:29 26 4
gpt4 key购买 nike

我正在使用 ListView/DataPager。

出于性能原因,我使用 ROW_NUMBER(SQl2005) 在数据库中对结果进行分页。

在我的 C# 代码中,一次只出现一页。我怎么能告诉 DataPager 我有更多的行确实在我的列表中?

最佳答案

我创建了一个生成假默认 (T) 对象的类。工作正常:

public class PagedList<T> : IEnumerable<T>, ICollection
{
private IEnumerable<T> ActualPage { get; set; }
private int Total { get; set; }
private int StartIndex { get; set; }

public PagedList(int total, int startIndex, IEnumerable<T> actualPage)
{
ActualPage = actualPage;
Total = total;
StartIndex = startIndex;
}

public IEnumerator<T> GetEnumerator()
{
bool passouPagina = false;
for (int i = 0; i < Total; i++)
{
if (i < StartIndex || passouPagina)
{
yield return default(T);
}
else
{
passouPagina = true;
foreach (T itempagina in ActualPage)
{
i++;
yield return itempagina;
}
}
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

#region Implementation of ICollection

void ICollection.CopyTo(Array array, int index)
{
throw new NotSupportedException();
}

public int Count
{
get { return Total; }
}

object ICollection.SyncRoot
{
get { throw new NotSupportedException(); }
}

bool ICollection.IsSynchronized
{
get { throw new NotSupportedException(); }
}

#endregion
}

使用示例:

int totalRows = DB.GetTotalPeople();
int rowIndex = (currentPage-1)*pageSize;
List<Person> peoplePage = DB.GetPeopleAtPage(currentPage);

listview.DataSource = new PagedList(totalRows, rowIndex, peoplePage)
listView.DataBind();

关于c# - 如何将 DataPager 与数据库分页一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2447130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com