views:

228

answers:

2

The documentation only shows how to bind to an IEnumerable (which uses linq to page and sort) ... but I need to go against a sproc because expressing the query I'm working on with linq is proving to be a bit slow.

Can anyone provide any guidelines or pointers on what's the best way to do this?

+1  A: 

Are you talking about how to bind the Telerik grid to a result set emitted by a stored procedure, or how to implement paging in a stored procedure? It's not clear from your question.

The following T-SQL stored procedure is a very efficient implementation of paging. THE SQL optimiser can find the first ID very fast. Combine this with the use of ROWCOUNT, and you have an approach that is both CPU-efficient and read-efficient. For a table with a large number of rows, it certainly beats any approach that I've seen using a temporary table or table variable.

CREATE  PROCEDURE dbo.PagingTest
(
    @PageNumber int,
    @PageSize int
)
AS

DECLARE @FirstId int, @FirstRow int

SET @FirstRow = ( (@PageNumber - 1) * @PageSize ) + 1
SET ROWCOUNT @FirstRow

-- Add check here to ensure that @FirstRow is not
-- greater than the number of rows in the table.

SELECT   @FirstId = [Id]
FROM     dbo.TestTable
ORDER BY [Id]

SET ROWCOUNT @PageSize

SELECT   *
FROM     dbo.TestTable
WHERE    [Id] >= @FirstId
ORDER BY [Id]

SET ROWCOUNT 0
GO 
RoadWarrior
It was a mix of both questions. The question is how to implement paging *so that it works with the telerik grid* ... as it stands, all the demos I've seen just use linq so presumably it handles sorting and paging by using linq methods. This is a great example of a paging sproc ... thank you, now the next part of the question is as you mentioned, how to bind it so the grid communicates page number and page size to the sproc :-)
Joel Martinez
+1  A: 

You need to to use custom binding. The linked example shows how to get the current page.

korchev