C# – Total row count in GridView control using LinqDataSource and paging

asp.netcgridviewlinq

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?

Best Answer

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

Related Topic