locked
LinqDataSource with AutoPage, how to access Total Record Count? RRS feed

  • Question

  • hi,
    i've been using the LinqDataSource with a gridview, AutoPage is set to true.  The sql profiler confirms when viewing page 3 in the Gridview that the LinqDataSource sends 2 queries, one to count the total number of matching records (presumably for the Pager), and another to fetch the data. 

    i like to tell the user how many records they are browsing (not just the pagesize).  previously i could access the GridView.Rows.Count property to determine the number of rows without performing an additional SQL query.  but now since LINQ is limiting the select statement to GridView.PageSize, i can't access the total number of matching records. 

    i don't want to send another Count query to the database because LINQ has just done it.  is there any way to figure out how many total records there are?  ideally i was hoping to find something like LinqDataSource.TotalRecordCount Smile

    many thanks in advance.
    tim
    Tuesday, September 18, 2007 5:25 PM

Answers

  • When the databound control has paging enabled it uses DataSourceSelectArguments.RetrieveTotalRowCount to request the total row count from the datasource control.  The datasource then provides this value through the DataSourceSelectArguments.TotalRowCount property.  LinqDataSource and the databound controls don't provide access to this total row count value.

     

    One exception is the new ListView control when used with a DataPager.  During data binding, the ListView notifies the DataPager of the page properties and the total row count is available from the DataPager.TotalRowCount property.  So, once the ListView is databound, you could access this.

     

    Note that if you are doing your queries during the LinqDataSource's Selecting event, you could do the paging yourself.  In this case you would turn AutoPage off on the LinqDataSource and handle the DataSourceSelectArguments yourself.

    Thursday, September 20, 2007 12:23 AM

All replies

  • You might try to get the total records by GridView1.Page * GridView1.PageCount

    Wednesday, September 19, 2007 12:33 AM
  • hi. thanks for the reply.  i thought of that but it's only an estimate since the last page will usually be only partially full.  i want users to be able to depend on the row count. 

    i looked into the RetrieveTotalRowCount property in the LinqDataSource_Selecting event, i can set it to true and all, but i can't find out where to access that value.  it is -1 in the Selecting event itself, presumably because the data is not yet "Selected".  The LinqDataSource_Selected event argument doesn't provide access to this property.  is there any way i can get to it?

    thanks
    tim

    Wednesday, September 19, 2007 1:28 PM
  • When the databound control has paging enabled it uses DataSourceSelectArguments.RetrieveTotalRowCount to request the total row count from the datasource control.  The datasource then provides this value through the DataSourceSelectArguments.TotalRowCount property.  LinqDataSource and the databound controls don't provide access to this total row count value.

     

    One exception is the new ListView control when used with a DataPager.  During data binding, the ListView notifies the DataPager of the page properties and the total row count is available from the DataPager.TotalRowCount property.  So, once the ListView is databound, you could access this.

     

    Note that if you are doing your queries during the LinqDataSource's Selecting event, you could do the paging yourself.  In this case you would turn AutoPage off on the LinqDataSource and handle the DataSourceSelectArguments yourself.

    Thursday, September 20, 2007 12:23 AM
  • cool, thanks for the tip.  i think i will eventually use the Selecting event to do the query, so this is great advice. 
    i'll have a look at the listview as well, i'll have to learn it eventually!
    Thursday, September 20, 2007 7:40 AM
  • Come on, you must be kidding! The underlaying object (LinqDataSource) first launches a COUNT select statement. There must be a way to access it! Come on guys you have to give us a solution.
    Thursday, March 20, 2008 10:27 AM
  •  

    if you are binding the linqdatasource in code behind, i.e. the Selecting event handler, then u can get the count by e.Result.Count property of the eventargs.

     

    Thanks,

    Pranil

    Monday, May 5, 2008 8:05 PM
  • The LinqDataSourceStatusEventArgs will contain a TotalRowCount property, set for the Selected event, in the next release.

    Friday, May 9, 2008 10:20 PM
  • You can find Total Record Count by declaring a LinqDataSourceSelectEventArgs variable as a member of the class, and assign it to the e variable in the Selecting event of the LinqDataSource.

    Than you have total records in yourVariable.Arguments.TotalRowCount
    Friday, May 30, 2008 9:22 AM
  • this is excellent, i didn't think it was possible.  thanks for posting.  just to point out to anyone trying to do this, you can't access TotalRowCount in the LinqDataSource_Selecting event because the data is not fetched yet (you get -1), but it does work in the Selected event. here is a sample:

    Code Snippet

    private LinqDataSourceSelectEventArgs args;
    protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
         args = e;           
         e.Result = new Database().Table.Whatever...                      
    }

    protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
    {
         this.label1.Text = args.Arguments.TotalRowCount + " records";
    }


    • Proposed as answer by Jules.Bartow Friday, July 27, 2012 2:16 PM
    Friday, May 30, 2008 11:16 AM