Hi Smile,
Please refer Below sample.
You have to call this CalculateSumTotal() function where ever you want sum of total.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView runat="server" ID="gvOrders" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="ID" />
<asp:BoundField DataField="ShipName" HeaderText="Name" />
<asp:BoundField DataField="ShipCountry" HeaderText="Country" />
<asp:TemplateField>
<HeaderTemplate>
Bag No / Qty
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text="0" class="quantity form-control" Width="100"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Rate
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtRate" runat="server" Text="0" class="rate form-control" Width="100"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Discount
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtDiscount" runat="server" Text="0" class="discount form-control" Width="100"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Net Balance
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtNet" runat="server" Text="0" class="pay form-control" Width="100"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Paid
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtPaid" runat="server" Text="0" class="paid form-control" Width="100"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
RemBal
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtRem" runat="server" Text="0" class="rem form-control" Width="100"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text="0" class="total control-label"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<hr />
<table>
<tr>
<td>Total: </td>
<td>
<asp:TextBox runat="server" ID="txtTotal" /></td>
</tr>
</table>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function CalculateSumTotal() {
var grandTotal = 0;
$("[id*=lblTotal]").each(function () {
grandTotal = grandTotal + parseFloat($(this).html());
});
$('[id*=txtTotal]').val(grandTotal);
}
$(function () {
$("[id*=txtQuantity]").val("0");
$("body").on("change keyup", "[id*=txtQuantity],[id*=txtRate]", function () {
var row = $(this).closest("tr");
var qty = $("[id*=txtQuantity]", row);
var price = $("[id*=txtRate]", row);
var quantity = parseFloat($.trim($(qty).val()));
if (isNaN(quantity)) {
quantity = 0;
}
var rate = parseFloat($.trim($(price).val()));
if (isNaN(rate)) {
rate = 0;
}
$(qty).val(quantity);
$(price).val(rate);
$("[id*=lblTotal]", row).html(rate * quantity);
//Calculate and update Grand Total.
CalculateSumTotal();
});
});
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string conn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 OrderId, ShipName, ShipCountry,Freight,Freight FROM Orders", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvOrders.DataSource = dt;
gvOrders.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim conn As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conn)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 10 OrderId, ShipName, ShipCountry,Freight,Freight FROM Orders", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvOrders.DataSource = dt
gvOrders.DataBind()
End Using
End Using
End Using
End Using
End Sub
Screenshot