Search code examples
c#asp.net.netdatagridcustompaging

Total Number of records required in paged .NET datagrid control


I am using a data grid and has bound a data source with it.

I am trying to get the total number of records in the grid in overriden InitializePager method from pagedDataSource DataSourceCount.

I thought DataSourceCount returns number of records from SelectCountMethod of ObjectDataSource, but DataSourceCount is giving me the page size and not the total number of records, whereas when I debug and see in SelectCountMethod it is returning correct number of total Records.

I am not sure how to get the data from SelectCountMethod in DataGrid.


Solution

  • The DataSource has a Selected Event which is fired when the Select and the SelectCount method are executed. Even if its a bit ugly it is a way to get the count:

    protected void MyDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        var count = e.ReturnValue as int?;
        if (count.HasValue)
            litResults.Text = string.Format("Total results found {0}", count);
    }
    

    My Count method of the ObjectDataSource returns an int, so of the ReturnValue of the Selected EventArgs is an int, its the count.

    Hope this helps you.