Hi rajeesh,
Refer below sample.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True">
<Columns>
<asp:BoundField DataField="itemname" HeaderText="Item Name" />
<asp:BoundField DataField="price" HeaderText="Price" />
<asp:TemplateField HeaderText="Quantity">
<FooterTemplate>
<asp:Label ID="Label44" runat="server" Text="Sub Total"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Height="30px" Text='<%# Eval("obqty") %>'
Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text="0"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblGrandTotal" runat="server" Text="0"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button Text="Calculate" runat="server" OnClick="Button3_Click" />
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] {
new System.Data.DataColumn("itemname", typeof(string)),
new System.Data.DataColumn("obqty", typeof(int)),
new System.Data.DataColumn("price", typeof(int))});
dt.Rows.Add("Pen", 10, 100);
dt.Rows.Add("Pencil", 20, 100);
dt.Rows.Add("Eraser", 30, 100);
dt.Rows.Add("Highlighter", 40, 100);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
decimal grandTotal = 0;
foreach (GridViewRow row in GridView1.Rows)
{
decimal quantity = Convert.ToDecimal((row.FindControl("txtQuantity") as TextBox).Text);
decimal price = Convert.ToDecimal(row.Cells[1].Text);
decimal total = 0;
if (row.RowType == DataControlRowType.DataRow)
{
total = price * quantity;
(row.FindControl("lblTotal") as Label).Text = total.ToString();
}
grandTotal += total;
}
(GridView1.FooterRow.FindControl("lblGrandTotal") as Label).Text = grandTotal.ToString();
}
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 System.Data.DataTable = New System.Data.DataTable()
dt.Columns.AddRange(New System.Data.DataColumn() {New System.Data.DataColumn("itemname", GetType(String)), New System.Data.DataColumn("obqty", GetType(Integer)), New System.Data.DataColumn("price", GetType(Integer))})
dt.Rows.Add("Pen", 10, 100)
dt.Rows.Add("Pencil", 20, 100)
dt.Rows.Add("Eraser", 30, 100)
dt.Rows.Add("Highlighter", 40, 100)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim grandTotal As Decimal = 0
For Each row As GridViewRow In GridView1.Rows
Dim quantity As Decimal = Convert.ToDecimal((TryCast(row.FindControl("txtQuantity"), TextBox)).Text)
Dim price As Decimal = Convert.ToDecimal(row.Cells(1).Text)
Dim total As Decimal = 0
If row.RowType = DataControlRowType.DataRow Then
total = price * quantity
TryCast(row.FindControl("lblTotal"), Label).Text = total.ToString()
End If
grandTotal += total
Next
TryCast(GridView1.FooterRow.FindControl("lblGrandTotal"), Label).Text = grandTotal.ToString()
End Sub
Screenshot