In this article I will explain how to display sum of Columns total (Grand Total) in GridView Footer in ASP.Net using C# and VB.Net.
The GridView has paging enabled and the sum of values of a particular column on each page will be displayed in the GridView Footer Row as Grand Total.
For this article I am making use of the Microsoft’s Northwind Database. Download and install instructions are provided in the link below.
HTML Markup
The HTML Markup consists of an ASP.Net GridView. The ShowFooter property is set to true so that the GridView’s Footer Row is displayed.
Paging has been enabled for the GridView and OnPageIndexChanging event handler has been assigned.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPageIndexChanging" ShowFooter="true">
    <asp:BoundField DataField="OrderID" HeaderText="Order ID" ItemStyle-Width="60" />
    <asp:BoundField DataField="ProductName" HeaderText="Product Name" ItemStyle-Width="210" />
    <asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-Width="60" DataFormatString="{0:N2}"
        ItemStyle-HorizontalAlign="Right" />
You will need to import the following namespaces.
using System.Linq;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
Imports System.Linq
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Binding the GridView and calculating the Sum of Columns Total
The GridView is populated with records inside the Page Load event of the page. The Query gets records of Orders along with Products and their respective costs.
Once the GridView is populated, the Sum of the Price column is calculated and displayed in the Footer Row’s Cell as Grand Total.
protected void Page_Load(object sender, EventArgs e)
    if (!this.IsPostBack)
private void BindGrid()
    string query = "SELECT TOP 30 OrderID,";
    query += "(SELECT ProductName FROM Products WHERE ProductID = details.ProductId) ProductName,";
    query += "(Quantity * UnitPrice) Price";
    query += " FROM [Order Details] details";
    query += " ORDER BY OrderID";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
        using (SqlCommand cmd = new SqlCommand(query))
            using (SqlDataAdapter sda = new SqlDataAdapter())
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                    GridView1.DataSource = dt;
                    //Calculate Sum and display in Footer Row
                    decimal total = dt.AsEnumerable().Sum(row => row.Field<decimal>("Price"));
                    GridView1.FooterRow.Cells[1].Text = "Total";
                    GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
                    GridView1.FooterRow.Cells[2].Text = total.ToString("N2");
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
    End If
End Sub
Private Sub BindGrid()
    Dim query As String = "SELECT TOP 30 OrderID,"
    query += "(SELECT ProductName FROM Products WHERE ProductID = details.ProductId) ProductName,"
    query += "(Quantity * UnitPrice) Price"
    query += " FROM [Order Details] details"
    query += " ORDER BY OrderID"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand(query)
            Using sda As New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dt As New DataTable()
                    GridView1.DataSource = dt
                    'Calculate Sum and display in Footer Row
                    Dim total As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal)("Price"))
                    GridView1.FooterRow.Cells(1).Text = "Total"
                    GridView1.FooterRow.Cells(1).HorizontalAlign = HorizontalAlign.Right
                    GridView1.FooterRow.Cells(2).Text = total.ToString("N2")
                End Using
            End Using
        End Using
    End Using
End Sub
Implementing Paging in GridView
The following event handler handles the Pagination in GridView.
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    GridView1.PageIndex = e.NewPageIndex;
Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
    GridView1.PageIndex = e.NewPageIndex
End Sub
Display sum of Columns total in GridView Footer in ASP.Net using C# and VB.Net