views:

38

answers:

2

I know how to create and populate a datatable, which i have already done. Now, I simply want to display the datatable on a webform, but I also want to know how to paginate and sort it. Ultimately I do not want to bind this to a datagrid or gridview. I would like to learn how to programmatically do this myself.

Can you tell me how to display, paginate, and sort this datatable without using datagrid or gridview?

Thank you!

A: 

First you have to store your data into the session object/ viewstate /cache

Then generate table/ div according to your requirement and use loop for no. of records you want to display on screen * current page no +1.(current page no you have to store in viewsate or in hidden field)

For sorting use link on header (use table header with link) then for sorting use Linq. (after sorting data you have to replace your session object/ viewstate /cache object ) You can generate header using column’s name

Hasu
+1  A: 

Ideal pagination is done at database level. For that you create a SP which might take two parameters. One would be the no. of records you want to show on each page and another parameter would no. of page that your user is on.

For example - if your user is on page 1 and records per page is 20, then retrieve records from your db where row id is from 1 to 20. if user wants to see page 3 then retrieve row 41 to 60. If you do not have uniqueidentifier in your SQL table then you would have to use inner query with the help of ROW_NUMBER() function. Following is a link to paging using SQL Server ROW_NUMBER() function.

http://www.davidhayden.com/blog/dave/archive/2005/12/30/2652.aspx

Along side of this logic you can also combine sorting. That could be a third parameter to your SP. You can first sort your data in any way and then take a proper page out.

Pradeep