使用LinqDataSource和分页的GridView控件中的总行数
我在获取使用分页并以LinqDataSource作为数据源的Gridview中显示的项目的总行数时遇到问题。
我尝试了几种方法:
protected void GridDataSource_Selected(object sender, LinqDataSourceStatusEventArgs e) { totalLabel.Text = e.TotalRowCount.ToString(); }
每次返回-1.
protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e) { System.Collections.Generic.List<country> lst = e.Result as System.Collections.Generic.List<country>; int count = lst.Count; }
只给我当前页面的计数,而不是总数。
还有其他建议吗?
解决方案
回答
这些事件中返回的LinqDataSourceEventArgs在以下情况下返回-1:
-1 if the LinqDataSourceStatusEventArgs object was created during a data modification operation; -1 if you enabled customized paging by setting AutoPage to true and by setting RetrieveTotalRowCount to false.
请查看此处以获取更多信息,该表格位于底部,显示了设置不同的属性以返回行计数,但是看起来我们或者必须将AutoPage和AllowPage属性都设置为true或者都设置为false。
根据上面链接中的表格和我们提供的示例,将Autopage设置为false,但是AllowPaging设置为true,因此它将返回页面中的行数。
高温超导
回答
TotalRowCount属性仅对AutoPage和AllowPaging的某些值有效。它们都应该为真(在情况下),或者都应该为假。
请查看下一页以了解TotalRowCount属性的说明。
http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.linqdatasourcestatuseventargs.totalrowcount.aspx
回答
好了,我已经将AutoPage和AllowPaging设置为true。
我已通过在调试模式下检查其值来确认RetrieveTotalRowCount设置为true(找不到在何处更改其值)。
并且它仍然返回-1.
唯一缺少的是:
-1 if the LinqDataSourceStatusEventArgs object was created during a data modification operation;
我不太清楚这是什么意思。
我正在使用LinqDataSource的修改版本来启用一些自定义过滤,因此可能是问题所在。另一方面,在调试模式下搞乱时,我确实设法检查了arguments.TotalRowCount的值,它是正确的。但是,Selected事件中出现的值始终为-1.
回答
我陷入了同样的问题。我用以下代码行解决了我的问题
受保护的void LinqDataSourcePoints_Selected(对象发送者,LinqDataSourceStatusEventArgs e)
{
totalRecords =(e.Result作为List).Count;
}
解释:
1-将e.Result解析为数据源
2-获取计数。
为我完美地工作。
回答
试试这个,我已经测试过,它返回所有行。
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceStatusEventArgs e) { System.Collections.Generic.List<country> lst = e.Result as System.Collections.Generic.List<country>; int count = lst.Count; }
确保活动是"正在选择"