<p>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.</p>

<p>I've tried several approaches:</p>

<pre><code>protected void GridDataSource_Selected(object sender, LinqDataSourceStatusEventArgs e)  
{  
    totalLabel.Text = e.TotalRowCount.ToString();  
}
</code></pre>

<p>returns -1 every time.</p>

<pre><code>protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)  
{  
    System.Collections.Generic.List&lt;country&gt; lst  = e.Result as System.Collections.Generic.List&lt;country&gt;;  
    int count = lst.Count;  
}
</code></pre>

<p>only gives me the count for the current page, and not the total.</p>

<p>Any other suggestions?</p>

[{'answer_id': 27739, 'author': 'JamesSugrue', 'author_id': 1075, 'author_profile': 'https://Stackoverflow.com/users/1075', 'pm_score': 2, 'selected': False, 'text': '<p>The LinqDataSourceEventArgs returned in those events return -1 on these occasions:</p>\n\n<blockquote>\n  <p>-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. </p>\n</blockquote>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasourcestatuseventargs.totalrowcount.aspx" rel="nofollow noreferrer">Check here for more information</a> - 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.</p>\n\n<p>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.</p>\n\n<p>HTH</p>\n'}, {'answer_id': 27744, 'author': 'tgmdbm', 'author_id': 1851, 'author_profile': 'https://Stackoverflow.com/users/1851', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>chech out the following page for an explanation of the TotalRowCount property.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasourcestatuseventargs.totalrowcount.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasourcestatuseventargs.totalrowcount.aspx</a></p>\n'}, {'answer_id': 28357, 'author': 'Farinha', 'author_id': 2841, 'author_profile': 'https://Stackoverflow.com/users/2841', 'pm_score': 0, 'selected': False, 'text': "<p>Well, I've already set AutoPage and AllowPaging to true.\nI've confirmed that RetrieveTotalRowCount is set to true by checking its value in debug mode (couldn't find where to change its value).</p>\n\n<p>And it still returns -1.</p>\n\n<p>The only thing missing is:</p>\n\n<blockquote>\n  <p>-1 if the LinqDataSourceStatusEventArgs object was created during a data modification operation;</p>\n</blockquote>\n\n<p>and I'm not quite sure what this means.\nI 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.</p>\n"}, {'answer_id': 3341109, 'author': 'Mamoon ur Rasheed', 'author_id': 403004, 'author_profile': 'https://Stackoverflow.com/users/403004', 'pm_score': 0, 'selected': False, 'text': '<p>I was stuck with the same problem. I solved my problem with the following line of code</p>\n\n<p>protected void LinqDataSourcePoints_Selected(object sender, LinqDataSourceStatusEventArgs e)\n    {\n        totalRecords = (e.Result as List).Count;\n    }</p>\n\n<p>Explanation:\n1-Parse the e.Result as your data source\n2-Get the count.</p>\n\n<p>Work perfectly for me.</p>\n'}, {'answer_id': 3347416, 'author': 'Nick Kahn', 'author_id': 275390, 'author_profile': 'https://Stackoverflow.com/users/275390', 'pm_score': 0, 'selected': False, 'text': '<p>try this, i have tested and it returns all the rows.</p>\n\n<pre><code>  protected void LinqDataSource1_Selecting(object sender, LinqDataSourceStatusEventArgs e)\n        {\n           System.Collections.Generic.List&lt;country&gt; lst  = e.Result as System.Collections.Generic.List&lt;country&gt;;\n\n           int count = lst.Count;\n        }\n</code></pre>\n\n<p>make sure your event is "<strong>Selecting</strong>"</p>\n'}]
['https://Stackoverflow.com/questions/27711', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2841/']