【问题标题】:Total row count in GridView control using LinqDataSource and pagingGridView 控件中使用 LinqDataSource 和分页的总行数
【发布时间】:2010-09-06 20:42:14
【问题描述】:

我在获取使用 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;  
}

只给我当前页面的计数,而不是总数。

还有其他建议吗?

【问题讨论】:

    标签: c# asp.net linq gridview


    【解决方案1】:

    在这些事件中返回的 LinqDataSourceEventArgs 在这些情况下返回 -1:

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

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

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

    HTH

    【讨论】:

      【解决方案2】:

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

      查看以下页面了解 TotalRowCount 属性。

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

      【讨论】:

        【解决方案3】:

        好吧,我已经将 AutoPage 和 AllowPaging 设置为 true。 我已通过在调试模式下检查其值确认 RetrieveTotalRowCount 设置为 true(找不到更改其值的位置)。

        它仍然返回 -1。

        唯一缺少的是:

        -1 如果 LinqDataSourceStatusEventArgs 对象是在数据修改操作期间创建的;

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

        【讨论】:

          【解决方案4】:

          我遇到了同样的问题。我用以下代码行解决了我的问题

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

          说明: 1-将 e.Result 解析为您的数据源 2-获取计数。

          为我工作完美。

          【讨论】:

            【解决方案5】:

            试试这个,我已经测试过了,它会返回所有的行。

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

            确保您的活动是“Selecting

            【讨论】:

            • 这是一个非常糟糕的解决方案,因为它会导致从服务器拉取所有记录。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-08-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-04-07
            相关资源
            最近更新 更多