Set label text to total number of gridview rows

I am using a stored procedure in sql database as a data source for SqlDataSourceControl on my .aspx page. Then I use SqlDataSourceControl as the data source for gridview on my page. Paging is set to true in the gridview. What I would like to do is set the label text to the total number of rows in the gridview. I can use this code

'labelRowCount.Text = GridView2.Rows.Count & " layers found" 

to return the number of results per page, but that does not give me the total. I looked through several places and could not find a solution.

+4
source share
3 answers

You must use the underlying data source that gridview (grid.DataSource) is bound to. For example, if you linked a grid to a datatable, then just enter the grid data source in datatable and use the rows.count property to get the total number of records. Another alternative would be to get a reference to the grid data source object before setting it to the grid so that you can directly get the record counter.

So for example (if you are tied to a DataTable)

 int count = ((DataTable)grid.DataSource).Rows.Count; 

Enjoy it!

+3
source

Put the event handler in "selected" for the SQL DataSource. This event handler has an argument of type SqlDataSourceStatusEventArgs. There AffectedRows is the number of rows for the entire data set (and not just what is shown on the current page). So understand this and write on your label:

 protected void SqlDataSource_Selected(object sender,SqlDataSourceStatusEventArgs e) { if (e.Exception != null) { // do something useful, then... e.ExceptionHandled = true; } else labelRowCount.Text = String.Format("{0} layers found", e.AffectedRows); } 
+2
source

GridView2.Rows saves only visible rows, so when the page size is 5, you get only 5 records. Since Doug suggested you set labelRowCount.Text ondatabound, and not on each postback, because in postback - when the data source is not mapped again - the data source will be nothing. Therefore, binding a grid to a data source can be a good place.

0
source

Source: https://habr.com/ru/post/1311766/


All Articles