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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 08:17:05  来源:igfitidea点击:

Total row count in GridView control using LinqDataSource and paging

提问by Farinha

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.

我在使用分页和 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?

还有其他建议吗?

回答by JamesSugrue

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 if you enabled customized paging by setting AutoPage to true and by setting RetrieveTotalRowCount to false.

-1 如果 LinqDataSourceStatusEventArgs 对象是在数据修改操作期间创建的;-1 如果您通过将 AutoPage 设置为 true 并将 RetrieveTotalRowCount 设置为 false 来启用自定义分页。

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

回答by tgmdbm

The TotalRowCount property is only valid for certain values of AutoPage and AllowPaging. They should both be true (in your case) or both be false.

TotalRowCount 属性仅对 AutoPage 和 AllowPaging 的某些值有效。它们都应该是真的(在你的情况下)或者都是假的。

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

回答by Farinha

Well, I've already set AutoPage and AllowPaging to 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).

好吧,我已经将 AutoPage 和 AllowPaging 设置为 true。我已经通过在调试模式下检查其值来确认 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. 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. But the value that comes out in the Selected event is always -1.

我不太确定这意味着什么。我正在使用 LinqDataSource 的修改版本来启用一些自定义过滤,所以这可能是问题所在。另一方面,在调试模式下,我确实设法检查了 arguments.TotalRowCount 的值,结果是正确的。但是 Selected 事件中出现的值始终为 -1。

回答by Mamoon ur Rasheed

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.

非常适合我。

回答by Nick Kahn

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"

确保您的活动是“选择