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; }
}
}
}