【问题标题】:Data paging with a data table使用数据表进行数据分页
【发布时间】:2011-05-28 21:20:51
【问题描述】:

我有一个存储许多记录的数据表。有一个名为 ID 的列自动递增,种子为 1。

我已经实现了一个分页控件。我知道我需要显示的第一条和最后一条记录的 ID,但我不知道如何从数据表中仅提取特定行,或者是否确实有可能。有可能吗,还是我需要使用数据集?

我使用的是名为 SiteFinity 的 CMS 产品,因此我无法访问数据库以使用 SQLDataAdapter 或类似产品,

【问题讨论】:

  • 你使用什么数据库(sql server,my sql)?据我了解,您需要 sql 分页,例如存储过程。

标签: c# pagination sitefinity


【解决方案1】:

这可能不是一个非常优雅的解决方案,但它可能对您有用。 我假设您将能够根据下面的代码找出您的起始索引和下一个索引。

DataTable dt = GetTable(); // this represents where your datatable is coming from
DataTable temp = dt.Clone();
DataRow[] rows = dt.Select("ID >= 1"); //insert the next begining index

int pageSize = 5;
int ctr = 0;
foreach(var row in rows)
{
    DataRow r = temp.NewRow();

    r["ID"] = row["ID"]; 
    r["Name"] = row["Name"];

   temp.Rows.Add(r); //its neccesary to create a new datarow or you'll get an exception if u add a row already belonging to a datatable

  ctr++;

  if(ctr == pageSize) break;
}

//at this point temp hold either 0 rows or <= pageSize rows and you can bind your control to it.

【讨论】:

    猜你喜欢
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多