Home All Groups Group Topic Archive Search About

Row count for query through SqlDataSource and GridView

Author
20 Feb 2006 9:22 PM
RyanRounkles
I'm using a GridView (for paging) and SqlDataSource on an ASP.NET 2.0 page.
Everything is working, but I cannot find a clean way to get the total number
of results (like RowCount in a dataset). Right now I'm running the query a
second time in a SqlDataAdapter, which I find really hack-ish. There must be
a way to get the TOTAL number of rows in a result (not just the number of
rows on this page).

Thanks in Advance,

Ryan

Author
20 Feb 2006 11:33 PM
Phillip Williams
You can get a record count if you handle the Selected event of the datasource
object that is bound to the GridView, e.g.
    void odsCustomersList_Selected(object sender,
ObjectDataSourceStatusEventArgs e)
    {
        //notice that this datasource returns a DataView
        lblCount1.Text = "Total Record count = " +
((DataView)e.ReturnValue).Count.ToString();
    }

Depending on the type of DataSource object that you use the AffectedRows
property of the eventargs might work or not.  For the
ObjectDataSourceStatusEventArgs it will work: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasourcestatuseventargs.affectedrows.aspx

For the SqlDataSourceEventArgs, it will work if you do not have this
DataSourceMode="DataReader" in the markup, otherwise you will get 0 http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasourcestatuseventargs.affectedrows.aspx


Show quoteHide quote
"RyanRounkles" wrote:

> I'm using a GridView (for paging) and SqlDataSource on an ASP.NET 2.0 page.
> Everything is working, but I cannot find a clean way to get the total number
> of results (like RowCount in a dataset). Right now I'm running the query a
> second time in a SqlDataAdapter, which I find really hack-ish. There must be
> a way to get the TOTAL number of rows in a result (not just the number of
> rows on this page).
>
> Thanks in Advance,
>
> Ryan