Hi indradeo,
Check this sample. now take its reference.
HTML
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" OnRowDataBound="RowDataBound" ShowFooter="true">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Items" HeaderText="Products" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
<asp:BoundField DataField="Total" HeaderText="Total" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("ID"),
new DataColumn("Items"),
new DataColumn("Price"),
new DataColumn("Quantity"),
new DataColumn("Total")
});
dt.Rows.Add("1", "Apple", "100", "1");
dt.Rows.Add("2", "Orange", "200", "2");
dt.Rows.Add("3", "Grapes", "300", "3");
this.gvOrders.DataSource = dt;
this.gvOrders.DataBind();
}
}
decimal grandTotal = 0;
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int price = Convert.ToInt32(e.Row.Cells[2].Text);
int quantity = Convert.ToInt32(e.Row.Cells[3].Text);
int total = price * quantity;
e.Row.Cells[4].Text = Convert.ToString(total);
grandTotal += total;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[3].Text = "Total";
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[4].Text = Convert.ToString(grandTotal);
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("ID"), New DataColumn("Items"), New DataColumn("Price"), New DataColumn("Quantity"), New DataColumn("Total")})
dt.Rows.Add("1", "Apple", "100", "1")
dt.Rows.Add("2", "Orange", "200", "2")
dt.Rows.Add("3", "Grapes", "300", "3")
Me.gvOrders.DataSource = dt
Me.gvOrders.DataBind()
End If
End Sub
Private grandTotal As Decimal = 0
Protected Sub RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim price As Integer = Convert.ToInt32(e.Row.Cells(2).Text)
Dim quantity As Integer = Convert.ToInt32(e.Row.Cells(3).Text)
Dim total As Integer = price * quantity
e.Row.Cells(4).Text = Convert.ToString(total)
grandTotal += total
End If
If e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(3).Text = "Total"
e.Row.Cells(3).HorizontalAlign = HorizontalAlign.Right
e.Row.Cells(4).Text = Convert.ToString(grandTotal)
End If
End Sub
Screenshot