在select查询中使用order by时,按页面顺序对gridview列进行排序

编程入门 行业动态 更新时间:2024-10-26 01:33:24
本文介绍了在select查询中使用order by时,按页面顺序对gridview列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨 我正在将数据从sqlserver提取到gridview. 我正在使用按国家/地区排序"对gridview列(国家/地区代码)进行排序 和按代码排序" 在查询中.我的意思是,我不使用datview.sort属性进行排序. 排序和分页一切正常... 我的问题是: 当我转到nextpage(page2)并单击标题进行排序时,它正在对所有数据进行排序.我只需要对page2中的数据进行排序.我知道原因是排序是从数据库而不是网格中进行的. 我该如何实现这一目标....请尽快提供帮助 感谢

解决方案

这里是存储proc调用的示例,您可以在其中传递页码和计数或返回的行以及"IN"参数.如有必要,您可以将其更改为您的国家代码,例如. 键是< b> SELECT ROW_NUMBER()OVER(按名称排序)AS行</b> 在GridView上的网格排序事件中,添加对DB的调用,以仅返回您需要的数据. 如果您有大数据集,这将更快地工作,因此您不需要将所有数据仅连接一小块... < pre> 创建过程[dbo].[sp_test] @list nvarchar(256), @start int, @count int AS 开始 宣告@sql nvarchar(256) 宣告@params nvarchar(256) 宣告@end int SET @end = @start + @count-1 SET @params = N''@开始整数,@count整数,@end整数'' SET @sql = N'' SELECT ID,类型,名称 从测试 在哪里输入ID( 从选择ID (SELECT ROW_NUMBER()OVER(按名称排序)AS行,ID 从测试 在哪里输入(''+ @list + N'') )PAGED 行> = @开始AND行< = @end ) '' 打印@sql EXEC SP_EXECUTESQL @ sql,@ params,@ start,@ count,@ end </pre>

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

解决方案

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>

更多推荐

在select查询中使用order by时,按页面顺序对gridview列进行排序

本文发布于:2023-10-23 13:39:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   页面   select   order   gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!