GridView (RadGrid) 和自定义分页
好的,所以我正在尝试在 Telerik RadGrid 上进行自定义分页(类似于 asp:Gridview
),但我仍然遇到困难。 (我的问题的第一部分在此处得到了回答)
所以我有实施了该建议。我使用以下存储过程
ALTER PROCEDURE [dbo].[bt_HealthMonitor_GetAll]
(
@StartRowIndex int,
@MaximumRows int
)
AS
SET NOCOUNT ON
Select
RowNum,
[ID],
[errEx],
[errURL],
[errSource],
[errUser],
[errMessage],
[errIP],
[errBrowser],
[errOS],
[errStack],
[errDate],
[errNotes]
From
(
Select
[ID],
[errEx],
[errURL],
[errSource],
[errUser],
[errMessage],
[errIP],
[errBrowser],
[errOS],
[errStack],
[errDate],
[errNotes],
Row_Number() Over(Order By [ID]) As RowNum
From dbo.[bt_HealthMonitor] t
)
As DerivedTableName
Where RowNum Between @StartRowIndex And (@StartRowIndex + @MaximumRows)
Order By [ID] Desc
然后另一个存储过程来获取记录计数
ALTER PROCEDURE [dbo].[bt_HealthMonitor_GetRecordCount]
AS
SET NOCOUNT ON
return (Select Count(ID) As TotalRecords From bt_HealthMonitor)
我使用 LINQ to SQL 绑定到我的 RadGrid
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
Dim startRowIndex As Integer = (RadGrid1.CurrentPageIndex * RadGrid1.PageSize)
Dim maximumRows As Integer = RadGrid1.PageSize
Dim HealthMonitorDC As New DAL.HealthMonitorDataContext
Dim r = HealthMonitorDC.bt_HealthMonitor_GetAll(startRowIndex, maximumRows)
RadGrid1.DataSource = r
End Sub
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim HealthMonitorDC As New DAL.HealthMonitorDataContext
Dim count = HealthMonitorDC.bt_HealthMonitor_GetRecordCount()
RadGrid1.MasterTableView.VirtualItemCount = count.ReturnValue
RadGrid1.VirtualItemCount = count.ReturnValue
End Sub
但我遇到的问题是网格仅获取前 10 行(如预期)但是我需要获取它,以便它能够识别表中有 200 行,以便显示分页图标。
如果我使用下拉列表显示 50 条记录,那么会显示 50 条记录,但仍然没有分页图标让我转到下 50 条记录。
我做错了什么?
Ok, so I'm trying to get my custom paging going on the Telerik RadGrid (similar to the asp:Gridview
), but I'm still hitting a wall. (the first part of my question was answered here)
So I have implemented the suggestion. I use the following Stored Proc
ALTER PROCEDURE [dbo].[bt_HealthMonitor_GetAll]
(
@StartRowIndex int,
@MaximumRows int
)
AS
SET NOCOUNT ON
Select
RowNum,
[ID],
[errEx],
[errURL],
[errSource],
[errUser],
[errMessage],
[errIP],
[errBrowser],
[errOS],
[errStack],
[errDate],
[errNotes]
From
(
Select
[ID],
[errEx],
[errURL],
[errSource],
[errUser],
[errMessage],
[errIP],
[errBrowser],
[errOS],
[errStack],
[errDate],
[errNotes],
Row_Number() Over(Order By [ID]) As RowNum
From dbo.[bt_HealthMonitor] t
)
As DerivedTableName
Where RowNum Between @StartRowIndex And (@StartRowIndex + @MaximumRows)
Order By [ID] Desc
Then another stored procedure to get the record count
ALTER PROCEDURE [dbo].[bt_HealthMonitor_GetRecordCount]
AS
SET NOCOUNT ON
return (Select Count(ID) As TotalRecords From bt_HealthMonitor)
And I'm using LINQ to SQL to bind to my RadGrid
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
Dim startRowIndex As Integer = (RadGrid1.CurrentPageIndex * RadGrid1.PageSize)
Dim maximumRows As Integer = RadGrid1.PageSize
Dim HealthMonitorDC As New DAL.HealthMonitorDataContext
Dim r = HealthMonitorDC.bt_HealthMonitor_GetAll(startRowIndex, maximumRows)
RadGrid1.DataSource = r
End Sub
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim HealthMonitorDC As New DAL.HealthMonitorDataContext
Dim count = HealthMonitorDC.bt_HealthMonitor_GetRecordCount()
RadGrid1.MasterTableView.VirtualItemCount = count.ReturnValue
RadGrid1.VirtualItemCount = count.ReturnValue
End Sub
But the problem I'm experiencing is that the grid only grabs the first 10 rows (as expected) but I need to get it so that it will recognize that there are 200 rows in the table so that the paging icons show up.
If I use the dropdownlist to display 50 records, then 50 show up, but still no paging icons to get me to the next 50.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要告诉网格总共有多少条记录。这是通过设置网格的 VirtualItemCount 属性来完成的(您必须查询记录总数)。
有关详细信息,请查看 文档页面或参考自定义分页的在线演示。
You need to tell the grid how many records there are in total. This is done by setting the grid's
VirtualItemCount
property (you will have to query the total number of records).For details, have a look at the documentation page or refer to the online demo for custom paging.
Martin 关于 VirtualItemCount 的说法是正确的。实现这一点最简单的地方是在 NeedDataSource 事件中。
请记住,您需要在其中添加一些逻辑以考虑最后一页上的较少记录。这意味着,如果您有 14 条记录,每页 5 条,您需要确保您的逻辑仅尝试在上次调用时检索 4 条记录。
我是这样做的(使用通用列表):
显然,如果您只是从数据库中检索记录,那么您将希望将其作为数据访问调用的一部分来执行。
Martin is correct regarding VirtualItemCount. The easiest place to implement this is in the NeedDataSource event.
Remember that you'll need to put some logic in there to account for fewer records on the last page. That means that if you have 14 records with 5 per page, you want to make sure your logic only tries to retrieve 4 records on the last call.
Here's how I did it (using a generic list):
Obviously, you'll want to do this as part of your data access call if you're only retrieving the records from the database as you go.
您还可以使用 ObjectDataSource 来实现。
http://www.unboxedsolutions.com/sean/archive/ 2005/12/28/818.aspx
即使用 RadGrid 与 ObjectDataSource 和 CustomPaging 即分页逻辑需要您自己实现。
ObjectDataSource也有两个方法
1. SelectMethod(可以指定返回数据的方法)
2. SelectCountMethod(您可以指定返回总计数的方法)
You can implement also using the ObjectDataSource.
http://www.unboxedsolutions.com/sean/archive/2005/12/28/818.aspx
i.e using RadGrid with ObjectDataSource with CustomPaging i.e Paging logic needs to implemented on your own.
Also ObjectDataSource has two methods
1. SelectMethod (Where you can specify the method which returns the data)
2. SelectCountMethod (Where you can specify the method which returns the total Count)