2

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

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

我使用一个名为 SiteFinity 的 CMS 产品,所以我无权访问数据库以使用 SQLDataAdapter 或类似产品,

4

1 回答 1

2

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

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.
于 2010-12-21T16:24:27.693 回答