How do I get the number of rows in my paged GridView control?
-
08-07-2019 - |
Question
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.
OTHER TIPS
I think you need to get the rows count of the datasource.
If datasource is datatable then
dt.Rows.Count
will give the total number of rows where dt is the datatable object.
If it is a dataset then get the corresponding datatable and then take the rows count.
ds.Tables["tablename"].Rows.Count; // give the datatable name
or
ds.Tables[tableIndex].Rows.Count; // give the datatable index
where ds is the dataset object.