Hi test0101,
Check the below example and implement in your code.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap.min.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<%--<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>--%>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.3/css/responsive.bootstrap.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/responsive/2.2.3/js/dataTables.responsive.min.js"></script>
<%--<script type="text/javascript" src="https://cdn.datatables.net/responsive/2.2.3/js/responsive.bootstrap.min.js"></script>--%>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" CssClass="dt-responsive" class="display" Width="100%" runat="server" AutoGenerateColumns="false" ClientIDMode="Static">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Customer Id" />
<asp:BoundField DataField="Name" HeaderText="Customer Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<asp:TextBox ID="txtSalary1" runat="server" Text='<%# Eval("Salary") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary2">
<ItemTemplate>
<asp:TextBox ID="txtSalary2" runat="server" Text='<%# Eval("Salary1") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary=(1+2)">
<ItemTemplate>
<asp:TextBox ID="txtRowTotal" runat="server" Text='<%# Eval("TotalSalary") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<br />
<label for="tungay">
Total Received:<asp:TextBox ID="txtTotalReceived" ReadOnly="true" runat="server" />
</label>
<%-- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css" />--%>
<script type="text/javascript">
$(function () {
var table = $("[id*=GridView1]").DataTable();
$('#<%=GridView1.ClientID%> tbody').on("change keyup", ".child input", function () {
CalculateRunningTotal($(this).closest("tr"));
});
$("[id*=txtSalary1],[id*=txtSalary2]").on("change keyup", function () {
CalculateRunningTotal($(this).closest("tr"));
});
function CalculateRunningTotal(row) {
var sal = $(row).find($("[id*=txtSalary1]"));
var sal1 = $(row).find($("[id*=txtSalary2]"));
if (sal.val() == "") {
$(sal).val("0");
}
if (sal1.val() == "") {
$(sal1).val("0");
}
if (!isNaN(sal.val()) && !isNaN(sal1.val())) {
$(row).find("[id*=txtRowTotal]").val(parseFloat($(sal).val()) + parseFloat($(sal1).val()));
}
var grandTotal = 0;
$("[id*=txtRowTotal]").each(function () {
grandTotal = grandTotal + parseFloat($(this).val());
});
$("[id*=txtTotalReceived]").val(grandTotal.toString());
}
});
</script>
</form>
</body>
</html>
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("Id"),
new System.Data.DataColumn("Name"),
new System.Data.DataColumn("Country"),
new System.Data.DataColumn("Salary"),
new System.Data.DataColumn("Salary1"),
new System.Data.DataColumn("TotalSalary")});
dt.Rows.Add(1, "John Hammond", "United States", 70000, 70000, 140000);
dt.Rows.Add(2, "Mudassar Khan", "India", 40000, 40000, 80000);
dt.Rows.Add(3, "Suzanne Mathews", "France", 30000, 30000, 60000);
dt.Rows.Add(4, "Robert Schidner", "Russia", 50000, 50000, 100000);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.UseAccessibleHeader = true;
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
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("Id"),
New System.Data.DataColumn("Name"),
New System.Data.DataColumn("Country"),
New System.Data.DataColumn("Salary"),
New System.Data.DataColumn("Salary1"),
New System.Data.DataColumn("TotalSalary")})
dt.Rows.Add(1, "John Hammond", "United States", 70000, 70000, 140000)
dt.Rows.Add(2, "Mudassar Khan", "India", 40000, 40000, 80000)
dt.Rows.Add(3, "Suzanne Mathews", "France", 30000, 30000, 60000)
dt.Rows.Add(4, "Robert Schidner", "Russia", 50000, 50000, 100000)
GridView1.DataSource = dt
GridView1.DataBind()
GridView1.UseAccessibleHeader = True
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader
End If
End Sub