views:

5983

answers:

5

Hello,

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.

I've tried several approaches:

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

returns -1 every time.

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?

Thank in advance.

+3  A: 

The LinqDataSourceEventArgs returned in those events return -1 on these occasions:

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

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.

HTH

KiwiBastard
A: 

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.

chech out the following page for an explanation of the TotalRowCount property.

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

tgmdbm
A: 

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).

And it still returns -1.

The only thing missing is:

-1 if the LinqDataSourceStatusEventArgs object was created during a data modification operation;

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.

Farinha
A: 

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

Explanation: 1-Parse the e.Result as your data source 2-Get the count.

Work perfectly for me.

Mamoon ur Rasheed
A: 

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"

Abu Hamzah