Posted by : Anwar Hossain
Category : How to use grid view in c#

Displaying total column value in the Gridview Footer using c#.

Dear viewer I will show how to add grid view column value in FooterTemplate value using ASP.NET C#. I have shown a very simple example to add total row value in grid view. I have created a product class to provide data to gridview but real-time data will be fetched from database. In this cases I have added TotalPrice column value and displayed to gridview FooterTemplate as GrandTotal .

Displaying total Gridview column value


<asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" Width="50%"
  OnRowDataBound="gvProduct_RowDataBound" ShowFooter="True" CellPadding="4" ForeColor="#333333"
  <AlternatingRowStyle BackColor="White" />
  <asp:TemplateField HeaderText="Product Name" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <asp:TemplateField HeaderText="Proudct Quantity" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <asp:TemplateField HeaderText="Price" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <asp:TemplateField HeaderText="Total Price" ItemStyle-HorizontalAlign="Center">
  <asp:Label ID="lblToalPrice" runat="server" Font-Bold="true" Font-Size="Medium" Text="" />
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <asp:Label ID="lblGroupTotal" runat="server" Font-Bold="true" Font-Size="Medium" />
  <EditRowStyle BackColor="#7C6F57" />
  <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
  <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
  <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
  <RowStyle BackColor="#E3EAEB" />
  <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
   <SortedAscendingCellStyle BackColor="#F8FAFA" />
  <SortedAscendingHeaderStyle BackColor="#246B61" />
  <SortedDescendingCellStyle BackColor="#D4DFE1" />
  <SortedDescendingHeaderStyle BackColor="#15524A" />


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GridViewTotalFotterValue : System.Web.UI.Page
  protected void Page_Load(object sender, EventArgs e)
  if (!IsPostBack)
  decimal GrandTotal = 0;
  protected void gvProduct_RowDataBound(object sender, GridViewRowEventArgs e)
  if (e.Row.RowType == DataControlRowType.DataRow)
  Product entry = e.Row.DataItem as Product;
  e.Row.Cells[0].Text = entry.ProductName;
  e.Row.Cells[1].Text = entry.ProductQuantity.ToString();
  e.Row.Cells[2].Text = entry.ProductPrice.ToString();
   decimal Total = entry.ProductPrice * entry.ProductQuantity;
  Label lblToalPrice = e.Row.FindControl("lblToalPrice") as Label;
  lblToalPrice.Text = Total.ToString();
  GrandTotal += Total;
  else if (e.Row.RowType == DataControlRowType.Footer)
  Label lblGroupTotal = e.Row.FindControl("lblGroupTotal") as Label;
  lblGroupTotal.Text = " Grand Total : " + GrandTotal.ToString();
  private void LoadAllProduct()
  List<Product> list = new List<Product>();
  list.Add(new Product("Mother Board",5,8000));
  list.Add(new Product("Mouse", 10, 250));
  list.Add(new Product("Keyboard", 8, 300));
  list.Add(new Product("Headphone", 3, 2500));
  list.Add(new Product("Monitor", 3, 20000));
  gvProduct.DataSource = list;
  public class Product
  public Product(string ProductName, int ProductQuantity, decimal ProductPrice)
  _ProductName = ProductName;
  _ProductQuantity = ProductQuantity;
  _ProductPrice = ProductPrice;
  private string _ProductName;
  public string ProductName
  get { return _ProductName; }
  set { _ProductName = value; }
  private int _ProductQuantity;
  public int ProductQuantity
  get { return _ProductQuantity; }
   set { _ProductQuantity = value; }
  private decimal _ProductPrice;
  public decimal ProductPrice
  get { return _ProductPrice; }
  set { _ProductPrice = value; }

Out Put

Realted Article Headline

Gridview datetime format example in ASP.Net c#
Displaying total column value in the Gridview Footer using c#.
How to create grid view paging in using c#
How to bind Multi-Dimensional Arrays Data to gird view in using C#
How to bind Two-Dimensional Arrays Data to gird view in using C#
How to bind Single-Dimensional Arrays Data to gird view in using C#
how to export gridview data to pdf in c#
how to export gridview data to word in using c#
how to export gridview data to excel in c#
How to delete row from grid view showing confirmation JavaScript using c#
How to delete data from grid view using row deleting event in c#
How to bind data into gridview using code behind in c#
How to scroll gird view overflow data in c#
How to Bind data into grid view control using c#

Article Category

How to create control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C# c # basic tutorial
How to use ajax toolkit in C#
How to use different types of validation control using c#
How to use grid view in c#
Protected by Copyscape Online Plagiarism Detection