Search code examples
asp.netgridviewrowssqldatasource

How do I get the number of rows in my paged GridView control?


The rows.count property on my GridView control only tells me how many rows are displayed on the screen, not the total number available.

The solution below is not working. I have an SqlDataSource and a GridView and neither can be cast into a dataset or datatable.


Solution

  • The SqlDataSource has an event called OnSelected that you can set to a method like the one below -

    protected void OnSelectedData(object sender, SqlDataSourceStatusEventArgs e)  
    {  
        // Set the record count label  
        RecordCountLabel.Text = "Total Records: " + e.AffectedRows;  
    }  
    

    Notice that the e.AffectedRows contains the total number of rows selected.