Tuesday, November 10, 2009

Determining Rowcount or Recordcount of a Gridview

If you have enabled Paging for a Gridview and attempt to get a count of all the records in the Gridview, you are in for a surprise.

Trying to use Gridview.Rows.Count will only show the number of Rows returned in the CURRENT page.  So if you have these properties set:

AllowPaging="True"
PageSize="10"

The highest value the Gridview.Rows.Count will return is 10.

Assuming you have used a SQLDataSource to populate the Gridview, you can use the SQLDataSource's Selected action to get the total record count that was retrieved.  For example:

Protected Sub sqlData_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles sqlData.Selected

GridView2.Caption = "Total records returned: " & e.AffectedRows.ToString

End Sub

No comments:

Post a Comment