c# - 使用 LinqDataSource 和分页的 GridView 控件中的总行数

标签 c# asp.net linq gridview

我在使用 Paging 和 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.

Check here for more information - 靠近底部的表格显示了要设置的不同属性以获取行数,但看起来您必须将 AutoPage 和 AllowPage 属性设置为 true 或都为 false。

根据上面链接中的表格和您提供的示例,您将 Autopage 设置为 false,但将 AllowPaging 设置为 true,因此它返回页面中的行数。

HTH

关于c# - 使用 LinqDataSource 和分页的 GridView 控件中的总行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27711/

相关文章:

c# - 如何替换 richtextbox 中插入符号位置下的单词? - 实现拼写检查建议

c# - 为什么公共(public)类不能从不太可见的类继承?

c# - 根据另一个查询的结果过滤一个 linq 查询

c# - 是否可以将 await 与使用自定义函数的查询一起使用

c# - 如何删除所有成员属性但保留空行?

c# - 通过 javascript 在 C# 中停止事件

asp.net - 页面刷新时再次触发 GridView_RowCommand 事件

asp.net - 当我在母版页中调用 jquery 时出错

c# - 未找到或更改 Linq 行

C# 规范文件名