简体   繁体   English

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

[英]Total row count in GridView control using LinqDataSource and paging

I'm having a problem obtaining the total row count for items displayed in a Gridview using Paging and with a LinqDataSource as the source of data. 我在使用Paging和LinqDataSource作为数据源获取Gridview中显示的项目的总行数时遇到问题。

I've tried several approaches: 我尝试了几种方法:

protected void GridDataSource_Selected(object sender, LinqDataSourceStatusEventArgs e)  
{  
    totalLabel.Text = e.TotalRowCount.ToString();  
}

returns -1 every time. 每次返回-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;  
}

only gives me the count for the current page, and not the total. 只给我当前页面的计数,而不是总数。

Any other suggestions? 还有其他建议吗?

The LinqDataSourceEventArgs returned in those events return -1 on these occasions: 在这些情况下,在这些事件中返回的LinqDataSourceEventArgs返回-1:

-1 if the LinqDataSourceStatusEventArgs object was created during a data modification operation; -1如果在数据修改操作期间创建了LinqDataSourceStatusEventArgs对象; -1 if you enabled customized paging by setting AutoPage to true and by setting RetrieveTotalRowCount to false. 如果通过将AutoPage设置为true并将RetrieveTotalRowCount设置为false来启用自定义分页,则返回-1。

Check here for more information - the table towards the bottom, shows different properties to set to get the rowcount back, but it looks like you either have to set AutoPage and AllowPage properties to either both true or both false. 请在此处查看更多信息 - 底部的表格,显示要设置以获取行计数的不同属性,但看起来您必须将AutoPage和AllowPage属性设置为true或两者都为false。

Judging by the table in the link above and the example you provide you have Autopage set to false, but AllowPaging set to true, therefore it is returning the amount of rows in the page. 根据上面链接中的表和您提供的示例判断Autopage设置为false,但AllowPaging设置为true,因此它返回页面中的行数。

HTH HTH

The TotalRowCount property is only valid for certain values of AutoPage and AllowPaging. TotalRowCount属性仅对AutoPage和AllowPaging的某些值有效。 They should both be true (in your case) or both be false. 它们都应该是真的(在你的情况下)或两者都是假的。

chech out the following page for an explanation of the TotalRowCount property. 请阅读以下页面,了解TotalRowCount属性的说明。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasourcestatuseventargs.totalrowcount.aspx http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasourcestatuseventargs.totalrowcount.aspx

Well, I've already set AutoPage and AllowPaging to true. 好吧,我已经将AutoPage和AllowPaging设置为true。 I've confirmed that RetrieveTotalRowCount is set to true by checking its value in debug mode (couldn't find where to change its value). 我已经通过在调试模式下检查其值来确认RetrieveTotalRowCount设置为true(无法找到更改其值的位置)。

And it still returns -1. 它仍然返回-1。

The only thing missing is: 唯一缺少的是:

-1 if the LinqDataSourceStatusEventArgs object was created during a data modification operation; -1如果在数据修改操作期间创建了LinqDataSourceStatusEventArgs对象;

and I'm not quite sure what this means. 而且我不太清楚这意味着什么。 I am using a modified version of the LinqDataSource to enable some custom filtering, so that might be the problem. 我正在使用LinqDataSource的修改版本来启用一些自定义过滤,这可能是问题所在。 On the other hand, while messing around in debug mode I did manage to check the value of the arguments.TotalRowCount and it was correct. 另一方面,虽然在调试模式下乱搞,我确实设法检查arguments.TotalRowCount的值,它是正确的。 But the value that comes out in the Selected event is always -1. 但是Selected事件中出现的值始终为-1。

I was stuck with the same problem. 我遇到了同样的问题。 I solved my problem with the following line of code 我用以下代码行解决了我的问题

protected void LinqDataSourcePoints_Selected(object sender, LinqDataSourceStatusEventArgs e) { totalRecords = (e.Result as List).Count; protected void LinqDataSourcePoints_Selected(object sender,LinqDataSourceStatusEventArgs e){totalRecords =(e.Result as List).Count; } }

Explanation: 1-Parse the e.Result as your data source 2-Get the count. 说明:1 - 将e.Result解析为数据源2 - 获取计数。

Work perfectly for me. 对我来说很完美。

try this, i have tested and it returns all the rows. 试试这个,我已经测试过,它会返回所有行。

  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;
        }

make sure your event is " Selecting " 确保你的活动是“ 选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM