Joshua Dale
Joshua Dale

Reputation: 1793

GridView Databinding and Paging

What is the best way to retrieve records from the database? Currently we are grabbing all of them, caching them, and binding them to our GridView control. We incorporate paging using this control. So what would be better? Retrieving all the records like we are currently doing, or just retrieving the records needed using an index and row count.

Upvotes: 1

Views: 780

Answers (2)

Stefan
Stefan

Reputation: 1739

Like klabranche said, it depends on the amount of rows you're talking about. For up to a couple of hundred, you approach is probably fine.

If you're talking about thousands, one option is using the ASP ObjectDataSource. It lets you specify separate methods for getting the row count and the actual rows for the current page:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.aspx

Upvotes: 0

Kevin LaBranche
Kevin LaBranche

Reputation: 21098

That kind of depends on how much data you are talking about. A few dozen to a few hundred and your current solution will likely suffice. Start getting into several hundred to thousands and you may want to look into paging with new stuff in SQL 2005 like the Row_Number and Rowcount features.

Here's a small run through on it: http://www.asp.net/LEARN/data-access/tutorial-25-cs.aspx

There are several ways to do it but this should get you started at least on considering what you should do.

You could even consider just capping how many records are returned by using the Top syntax IF of course you are using SQL Server. We have done that before and informed users to refine their search if the max result count was reached.

You could throw together a quick test using the above SQL 2005 functionality to see how your performance does and decide from there.

Upvotes: 1

Related Questions