Jonas Axelsson Jonas Axelsson - 2 years ago 127
ASP.NET (C#) Question

How to apply paging to a dataset?

How can I apply paging to a dataset? I have a method in which I dynamically build a table from a dataset. Querying the database from which 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 want to 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).

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download