Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am fetching the data from the sqlserver to the gridview.
i am sorting the gridview columns(country,code)using "order by country"
and "order by code"
in the query. I mean, iam not sorting using the datview.sort property.
sorting and paging all working fine...

my problem is:

when i go to nextpage(page2) and clicked on header to sort, it is sorting all the data. i need to sort only the data in page2. i know the reason is the sorting is from database not in the grid.

how can I achieve this in my case....please help ASAP

thanks
Posted
Updated 8-Jun-10 23:02pm
v2
Comments
Sandeep Mewara 9-Jun-10 2:10am    
Your data is already sorted on Page2, what for you need a sorting again?

1 solution

Here is an example of stored proc call where you can pass page number and count or rows returned as well as "IN" parameters. If needed, you can change them to your country code for example.

The key is <b>SELECT ROW_NUMBER() OVER (ORDER BY Name) AS Row</b>

in your GridView on grid sorting event add a call to DB to return only the data you'll need.

This is works faster if you have large data sets, so you don't need to wire all the data just a small chunk...

<pre>
CREATE PROCEDURE [dbo].[sp_test]
@list nvarchar(256),
@start int,
@count int
AS
BEGIN
DECLARE @sql nvarchar(256)
DECLARE @params nvarchar(256)
DECLARE @end int
SET @end = @start + @count - 1
SET @params = N'@start int, @count int, @end int'
SET @sql = N'
SELECT id, type, name
FROM TEST
WHERE ID IN (
SELECT ID FROM
(SELECT ROW_NUMBER() OVER (ORDER BY Name) AS Row, ID
FROM TEST
WHERE TYPE IN (' + @list + N')
) PAGED
WHERE Row >= @start AND Row <= @end
)
'
PRINT @sql
EXEC SP_EXECUTESQL @sql, @params, @start, @count, @end
</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900