In this article I will explain with an example, how to display Count of Total number of records in ASP.Net GridView using C# and VB.Net.
A Label will be placed below the GridView control and inside the OnDataBound event handler, the Count of Total number of records in ASP.Net GridView will be determined and displayed using C# and VB.Net.
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
The following HTML Markup consists of an ASP.Net GridView with four BoundField columns. In order to implement Paging in GridView, AllowPaging property is set to true and OnPageIndexChanging event has been handled.
Below the GridView, there is a Label control which will be used to display the Count of Total number of records in the GridView.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPageIndexChanging" PageSize="10" OnDataBound="OnDataBound">
<asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="Customer ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
<asp:BoundField ItemStyle-Width="150px" DataField="Country" HeaderText="Country" />
<asp:Label ID="lblTotal" runat="server" />
You will need to import the following namespaces.
Populating GridView from Database
Inside the Page Load event of the page, the GridView is populated with records from the Customers table of the Northwind Database.
protected void Page_Load(object sender, EventArgs e)
private void BindGrid()
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, ContactName, City, Country FROM Customers"))
using (SqlDataAdapter sda = new SqlDataAdapter())
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
GridView1.DataSource = dt;
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT CustomerId, ContactName, City, Country FROM Customers")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
GridView1.DataSource = dt
Implementing Paging in GridView
The OnPageIndexChanging event handler is executed when a page is changed inside the GridView.
The value of the PageIndex of the Page which was clicked is present inside the NewPageIndex property of the GridViewPageEventArgs object and it is set to the PageIndex property of the GridView and the GridView is again populated by calling the BindGrid function.
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
GridView1.PageIndex = e.NewPageIndex;
Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
GridView1.PageIndex = e.NewPageIndex
Displaying Count of Total number of records in GridView
The OnDataBound event handler is executed after the GridView is populated with records from Database.
The DataTable used to populate the GridView is referenced from the DataSource property of the GridView and then the Count of Total number of records in the GridView is displayed using Label control.
protected void OnDataBound(object sender, EventArgs e)
lblTotal.Text = "Total Rows: " + (GridView1.DataSource as DataTable).Rows.Count;
Protected Sub OnDataBound(sender As Object, e As EventArgs)
lblTotal.Text = "Total Rows: " & CType(GridView1.DataSource, DataTable).Rows.Count