views:

97

answers:

5

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 in 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...

A: 

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.

Gerrie Schenck
Hmm, if I did pull 10,000 results back (although its basically name, phone number, email) would it cause a problem, even if those results are filtered without the page ever being refreshed or anything?
SLC
If every result is 100 bytes (probably bigger) and you'd have 10.000 such lines in your viewstate you'll end up with almost 1 MB of data. ViewState is encoded, so it will not be this big on the page itself but you want to keep ViewState as small as possible.
Gerrie Schenck
A: 

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.

Paul
I kinda wanted a way of semi automating sorting, multiple pages etc. By saying use a repeater, do you mean basically pull my results back and display them on the page myself, using my own formatting?
SLC
A repeater is a control like a gridview, but you define the markup yourself. It is basically a glorified for loop with some page event stuff built in. If your data is tabular, then use a grid view I guess. unfortunately, you sill have to roll a lot of the sorting/ paging code yourself.
Paul
The control is irrelevant. How you return the data is all that matters. You can bind a repeater to something that returns a million rows just as easily as you can bind a GridView to something that returns a million rows. In fact, you can do as you advise (create queries to return only the results you need) and then bind that to a GridView.
Jason Berkan
+2  A: 

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()

Phil Jenkins
Sweet, looks useful and a bit complex, going to spend some time learning this
SLC
+3  A: 

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.

slugster
Ahhhhhhh I see. Good answer!
SLC
A: 

In that scenario better to use custom paging at Stored Procedure level check these articles

http://www.asp.net/Learn/Data-Access/tutorial-26-cs.aspx

http://aspnet.4guysfromrolla.com/articles/031506-1.aspx

in the result set, you will only get those result that required in gridview page.

Muhammad Akhtar