Partha
Partha

Reputation: 2192

Paging Mechanism

I am in the situation of working with large amount of records in a grid. Instead of, binding all records into the grid. why we should not bind set of records which are belongs to 5th page.

Upvotes: 0

Views: 477

Answers (2)

Prashanth
Prashanth

Reputation: 2434

As a good practice one should not bind all the records retrieved from a query directly to a control such as grid view , unless of course you are absolutely sure that the number of records are few.

This is because when the number of records are huge, the page loads slower,since binding takes a lot of time.

One approach that I have used in my previous project is to fetch the total number of records affected by the query. (Lets say they are 1000).

I then divide the number of records with the page size (say 100).

So now I provide pagination with pages (1-10).

When the user clicks on a particular page, I fetch records pertaining to that page using ROW_NUMBER() function in SQL.

The query will be something like

SELECT x, y, ROW_NUMBER() OVER(ORDER BY z asc) AS 'RowNumber'
FROM t WHERE RowNumber > lowerlimit and RowNumber < upperlimit.

Lowerlimit and upperlimit are calculated using the current page number and page size.

I hope this answers your question

Upvotes: 2

Patrick McDonald
Patrick McDonald

Reputation: 65411

You can enable paging on your GridView by setting AllowPaging to true and PageSize to the number of records you wish to display per page.

The advantage of paging is that you can bind the full data source to your GridView and allow the user to move between pages, it's much less programming on the server side to always work with the full data source.

EDIT:

It is also recommended that you don't put the entire dataset in the GridView view state, a better option would be to cache the data and rebind in the Page Load

Upvotes: 2

Related Questions