开发者

Gridview's in ASP.NET, surely there is a better way?

They seem to be so nasty.

I have a gridview, and the query it runs pulls back 10,000 results if no filters are set... and displays 10 of them on the first page. You press page 2, and then it does the query again... and again...

Isn't there a way to cache them? Isn't there a much easier way to bring i开发者_运维问答n all the results then filter them live without repeated queries? Must ASP.NET be all about a struggle against the gridview?

I would love to know if there is a better way...


Which grid are you using? GridViews will bind to anything that implements IEnumerable, so do you realise that you can fetch the data and explicitly bind it yourself? Similar to:

List<myDataObjects> data = executeMyQuery();
gridview.DataSource = data;
gridView.DataBind();

this way you can control when the data is fetched, and you can even cache it if required.


If you're using SQL Server 2005 and above, you can make use of the ROW_NUMBER() function in your query, which you can use to page the rows returned at source.

ScottGu has a detailed blog post here using a DataList, but this is equally applicable to GridViews

This blog entry has a more concise introduction to the specifics of ROW_NUMBER()


Instead of performing a query which returns everything in one big resultset, consider returning your results in a paged fashion.

You can put the results in the ViewState, but this isn't really advisable if you have a lot of data which can be returned.


Use a repeater and create your queries to only return the results you need. Your query will probably need to make use of parameters for resultsPerPage and PageNumber.

Gridviews connected to those datasets are good for 5 minute demos, but really shouldn't end up in production code. Nasty is good word for it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜