Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Paging GridView with ROW_NUMBER()

, 5 Feb 2006
This article explains how to implement custom paging in the GridView web control using the ROW_NUMBER() function of SQL Server 2005
PagingGridView_src.zip
Images
first.gif
GoToPage.gif
last.gif
next.gif
PageSize.gif
previous.gif
Refresh.gif
Search.gif
��USE [AdventureWorks]

GO

/****** Object:  StoredProcedure [dbo].[sp_GridView_RowNumber]    Script Date: 02/05/2006 10:56:37 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO



-- =============================================

-- Author:		Socrate

-- Create date: 2/5/2006

-- Description:	implements ROW_NUMBER function

-- =============================================

CREATE PROCEDURE [dbo].[sp_GridView_RowNumber]

(

	@PageNum int,

	@PageSize int,

	@TotalRowsNum int output

)

AS



BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from

	-- interfering with SELECT statements.

	SET NOCOUNT ON;



	-- Use ROW_NUMBER function

	WITH Salespeople_AdventureWorks As

	(

		SELECT 'RowNumber' = ROW_NUMBER() OVER(ORDER BY SalesYTD DESC), 'Name' = c.FirstName + ' ' + c.LastName, s.SalesYTD, a.PostalCode

		FROM Sales.SalesPerson s JOIN Person.Contact c on s.SalesPersonID = c.ContactID

		JOIN Person.Address a ON a.AddressID = c.ContactID

		WHERE ((TerritoryID IS NOT NULL) AND (s.SalesYTD <> 0))

	)





	-- Query result

	SELECT * 

	FROM Salespeople_AdventureWorks

	WHERE RowNumber BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize             

	ORDER BY SalesYTD DESC



	-- Returns total records number

	SELECT @TotalRowsNum = count(*) 

	FROM Sales.SalesPerson s JOIN Person.Contact c on s.SalesPersonID = c.ContactID

	JOIN Person.Address a ON a.AddressID = c.ContactID

	WHERE ((TerritoryID IS NOT NULL) AND (s.SalesYTD <> 0))



END





By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Socrate1
Web Developer
Canada Canada
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 5 Feb 2006
Article Copyright 2006 by Socrate1
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid