GridView summary on Footer
In Asp.Net GridView, displaying summary data is a common report requirement. In usual situations we have to display the total value of all gridrows in the Footer row. We can include this footer row in GridView in a simple way.

Default.aspx code:-

  

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="gvDetail" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnRowDataBound="gvDetail_RowDataBound">
        <Columns>
		  <asp:BoundField DataField="PId" HeaderText="PId" />
		  <asp:BoundField DataField="PName" HeaderText="Product Name" />
		
		  <asp:TemplateField HeaderText="Quantity">
			 <ItemTemplate>
				<div style="text-align: right;">
				<asp:Label ID="lblqty" runat="server" Text='<%# Eval("Quantity") %>' />
				</div>
			 </ItemTemplate>
			 <FooterTemplate>
				<div style="text-align: right;">
				<asp:Label ID="lblTotalqty" runat="server" />
				</div>
			 </FooterTemplate>
		  </asp:TemplateField>
		</Columns>
        <FooterStyle BackColor="#cccccc" Font-Bold="True" ForeColor="Black" HorizontalAlign="Left" />
    </asp:GridView>
    </div>
    </form>
</body>
</html>




Default.aspx.cs code:-

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default : System.Web.UI.Page
{
    int total = 0;
    string connectionString = "Data Source=PS-PC\\SANTOSH;Initial Catalog=dbSantoshTest;Integrated Security=True";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadRecord();
        }

    }
    protected void loadRecord()
    {
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from tblProduct", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        int count = ds.Tables[0].Rows.Count;
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {

            gvDetail.DataSource = ds;
            gvDetail.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvDetail.DataSource = ds;
            gvDetail.DataBind();
            int columncount = gvDetail.Rows[0].Cells.Count;

        }
    }
    protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblqy = (Label)e.Row.FindControl("lblqty");
            int qty = Int32.Parse(lblqy.Text);
            total = total + qty;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblTotalqty = (Label)e.Row.FindControl("lblTotalqty");
            lblTotalqty.Text = total.ToString();
        }
    }
}