开发者

gridview asp.net

开发者 https://www.devze.com 2023-01-27 21:34 出处:网络
I have a gridview which开发者_运维知识库 was working fine with a small dataset in development. In production it has to bind to thousands of records, which is making it slower to load. Is there a way t

I have a gridview which开发者_运维知识库 was working fine with a small dataset in development. In production it has to bind to thousands of records, which is making it slower to load. Is there a way to improve performance, like retrieving the data during gridview pageindex changing?


Also chances are you only want to bind it once. So you should (if not already):

if(!IsPostback)
{
    DatabindGridLogicHere();
}

This way your GridView will only have to hit the db the first time to get the data.


First and formost turn off ViewState.


You should tell your datasource to take less records and then enable paging in your grid and datasource.


You can enable "AllowPaging" property to true in your GridView and give a page size say 10. Then Write your data retrieval logic to return a batch of data instead of the whole set of data at once. When you write the SQL query make sure to order it by an ID.

Thus if the page index is 1 you can take the first batch of data by passing page index of 1 and the page size of 10.

Logic will be;

SELECT  [RequiredFields] 

FROM [YourDataSource] 

WHERE (Id>=((PageIndex-1)*pageSize) AND Id<(PageSize*pageIndex)) ORDER BY Id

In the first page it will return first set of entries of those Ids starting from 0 to 9. In the second page it returns entries of those Ids starting from 10 to 19 assuming the pageSize is 10. You can change the page size and the query logic as you wish.

But if sorting is enable then this will not produce accurate results.

0

精彩评论

暂无评论...
验证码 换一张
取 消