Data paging with a data table
I've got a datatable that stores a number of records. There is a column called ID which is autoincrememted with a seed of 1.
I have implemented a paging control. I know the ID of the first and last record that I need to display, but I don't know how to extract just specific rows from a datatable, or whether it is indeed possible. Is it possible, or do I need开发者_StackOverflow社区 to use a dataset for example?
Im using a CMS product called SiteFinity, so I dont have access to the DB to use a SQLDataAdapter or similar,
This might not be a very elegant solution but it might work for you. i'm assuming you'll be able to figure out your start index and next index based on the code below.
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.
精彩评论