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

Displaying total column value in the Gridview Footer using asp.net 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

HTML

<asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" Width="50%"
  OnRowDataBound="gvProduct_RowDataBound" ShowFooter="True" CellPadding="4" ForeColor="#333333"
  GridLines="None">
  <AlternatingRowStyle BackColor="White" />
  <Columns>
  <asp:TemplateField HeaderText="Product Name" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <FooterTemplate>
   </FooterTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Proudct Quantity" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Price" ItemStyle-HorizontalAlign="Center">
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Total Price" ItemStyle-HorizontalAlign="Center">
  <ItemTemplate>
  <asp:Label ID="lblToalPrice" runat="server" Font-Bold="true" Font-Size="Medium" Text="" />
  </ItemTemplate>
  <ItemStyle HorizontalAlign="Center"></ItemStyle>
  <FooterTemplate>
  <asp:Label ID="lblGroupTotal" runat="server" Font-Bold="true" Font-Size="Medium" />
  </FooterTemplate>
   </asp:TemplateField>
  </Columns>
  <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" />
  </asp:GridView>
 

CODE BEHIND

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)
  {
  LoadAllProduct();
 
  }
  }
  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;
  gvProduct.DataBind();
 
 
  }
 
  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

Gridview-add-colum-value-in-asp.net



Realted Article Headline

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

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net 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#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection