user1481183
user1481183

Reputation: 368

c# - gridview sorting/paging advice

I'm using a gridview to display large amounts of data.

The data source is stored in session, so that I can page/sort the gridview without having to take additional trips to the SQL server.

Is there a better way to page/sort the grid without having to store the data in session?

Upvotes: 0

Views: 1314

Answers (4)

David R Tribble
David R Tribble

Reputation: 12204

Assuming that you want to store the entire table in the session to avoid hitting the SQL server, one approach is to do your own paging/sorting of the data, putting the resulting single page's worth of items in a List<>, and then populate the DataGrid with that List.

You'll have to implement your own page-back/page-forward control, but this is not that difficult. These controls fill the List with a new page's worth of viewable data from the entire in-memory table each time you click a forward/backward button.

The advantage is that you have total control over the page of data that is displayed, without the DataGrid control trying to display more than you want.

But be sure that the data you are caching in the session does not go stale; i.e., that you're not displaying old data items that have been modified or deleted, or failing to displayed newly inserted items, since you last retrieved the table from the SQL server. Some kind of time-out might be appropriate, so that you refresh the data from the database every few minutes or so (instead of every time the screen is redisplayed).

Upvotes: 0

Shortys Oberto Dutari
Shortys Oberto Dutari

Reputation: 556

in my experience I recommend using a ObjectDataSource because you can work dynamically through reflection large blocks of data without cluttering the session. Additional you can use: select, insert and delete.

Upvotes: 0

Tyanna
Tyanna

Reputation: 719

I think it all depends on the amount of data you are working with. If you are working with a few hundred rows, I would suggest looking at the jQuery plugin DataTables. From your code point of view, you make a regular table, and then in the document.Ready event you apply the DataTables script. It has paging, sorting, and filtering. It's pretty powerful.

If you are working with a thousand or more rows, I would suggest doing paging and sorting server side. In this case, when the user pages, you go to the database again and only return the number of rows they are viewing at any given time. So if you only show 50 records on a page, your query should only return 50 records. This way is a bit more tricky, but will be should be faster for the user, and also doesn't run the risk of reaching the max page size.

Upvotes: 2

Jonathan Henson
Jonathan Henson

Reputation: 8206

You could programmatically make rows visible and invisible based on the pager button. Have the dataset grouped into blocks of the page size. When you hide one block and make the one you want visible, the other should pop into its place. You can do this all client side.

Upvotes: 0

Related Questions