views:

77

answers:

3

How can i apply paging to a dataset? I have a method in wich i dynamically build a table from a dataset. Querying the database from wich i get my dataset from is not an issue but iterating the datarows from the dataset is slow.

To increase performance when loading a page containing a lot of datarows i wanna apply paging ability.

A feature that would be good to have is ability for the user to set pagesize (how many rows to display on each page).

+1  A: 

If your data is single a single datable then you can use the AsEnumerable() extension method. That will return the data as an IEnumerable collection. You can then use the LINQ extension methods .Skip() and .Take().

IEnumerable<DataRow> MyDataPage = MyDataTable.AsEnumerable().Skip(100).Take(10);

The above code would give you rows 101 to 110 of MyDataTable and it would be an IEnumerable collection which you can bind just liek a data table. If you need it to be an actual DataTable you can just call CopyToDataTable() thus:

DataTable NewDT = MyDataPage.CopyToDataTable();

More detailed info is available here

Ben Robinson
A: 

You can take a GridView to show the data and use its paging ability: http://msdn.microsoft.com/en-us/library/5aw1xfh3%28v=VS.80%29.aspx

Tim Schmelter
Yes but on my company we don't use webcontrols such as gridview, listview, dataview, .... , .... We usally build up tables manually and dynamically in the code-behind.
EasyDot
A: 

Linq is best option.

ROW_NUMBER() is also an option in SQL server 2005

Maverick
Is this also possible in SQL server 2008?
EasyDot