Click here to Skip to main content
12,635,649 members (26,065 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads

Stats

152.5K views
4.5K downloads
82 bookmarked
Posted

Paging GridView with ROW_NUMBER()

, 5 Feb 2006 Ms-RL
This article explains how to implement custom paging in the GridView web control using the ROW_NUMBER() function of SQL Server 2005
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, along with any associated source code and files, is licensed under Microsoft Reciprocal License

Share

About the Author

Socrate1
Web Developer
Canada Canada
No Biography provided

You may also be interested in...

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