Hi smile,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="gvBudget" runat="server" AutoGenerateColumns="False" Width="100%"
Class="table table-striped table-bordered table-hover" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" ShowFooter="True" OnRowDataBound="gvBudget_RowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reg No.">
<HeaderStyle HorizontalAlign="Left" Width="10%" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
<ItemTemplate>
<asp:Label ID="lbl_No" runat="server" Text='<%# Eval("AdmissionNo") %>' Width="50"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SName" HeaderText="Student Name">
<HeaderStyle HorizontalAlign="Left" Width="10%" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="FName" HeaderText="Father Name">
<HeaderStyle HorizontalAlign="Left" Width="10%" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="AcademicName" HeaderText="Session">
<HeaderStyle HorizontalAlign="Left" Width="10%" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
</asp:BoundField>
<asp:TemplateField HeaderText="Recievable">
<ItemTemplate>
<asp:TextBox ID="txtReci" runat="server" Text='<%# Bind("Recievable") %>' Width="100%"
class="field1 form-control" autocomplete="off">
</asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="10%" />
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Arrears">
<ItemTemplate>
<asp:TextBox ID="txtArr" runat="server" Text='<%# Bind("RemBal") %>' Width="100%"
class="field2 form-control" autocomplete="off">
</asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="10%" />
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount">
<ItemTemplate>
<asp:TextBox ID="txtDis" runat="server" Width="100%" class="field3 form-control"
autocomplete="off">
</asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="10%" />
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Net Balance">
<ItemTemplate>
<asp:TextBox ID="txtNet" runat="server" Width="100%" class="field4 form-control"
autocomplete="off">
</asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="10%" />
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid">
<ItemTemplate>
<asp:TextBox ID="txtPaid" runat="server" Width="100%" class="field5 form-control"
autocomplete="off">
</asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="10%" />
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Rem Bal">
<ItemTemplate>
<asp:TextBox ID="txtRem" runat="server" Text="0" Width="100%" class="field6 form-control"
autocomplete="off">
</asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="10%" />
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:TemplateField>
<asp:BoundField HeaderText="Total" />
</Columns>
<FooterStyle Font-Bold="True" CssClass="totaloffield" />
</asp:GridView>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=txtPaid]").keyup(function () {
var rowtotal = $(this).closest('tr').find('[id*=txtNet]').val();
var paid = $(this).val();
$(this).closest('tr').find('[id*=txtRem]').val(parseFloat(rowtotal) - parseFloat(paid));
});
$(".field1").keyup(function () {
var total = 0;
$(".field1").each(function () {
total += parseFloat($(this).val());
});
$(".totaloffield td").eq(1).text(total);
//calculate total for current row
var val1 = $(this).val() == "" ? 0 : $(this).val();
var val2 = $(this).parent().parent().find(".field2").val() == "" ? 0 : $(this).parent().parent().find(".field2").val();
var val3 = $(this).parent().parent().find(".field3").val() == "" ? 0 : $(this).parent().parent().find(".field3").val();
var rowtotal = parseFloat(!isNaN(val1) ? val1 : 0) + parseFloat(!isNaN(val2) ? val2 : 0) - parseFloat(!isNaN(val3) ? val3 : 0);
$(this).parent().siblings().last().text(rowtotal);
$(this).closest('tr').find('[id*=txtRem]').val(rowtotal);
});
$(".field2").keyup(function () {
var total = 0;
$(".field2").each(function () {
total += parseFloat($(this).val());
});
$(".totaloffield td").eq(2).text(total);
//calculate total for current row
var val1 = $(this).parent().parent().find(".field1").val() == "" ? 0 : $(this).parent().parent().find(".field1").val();
var val2 = $(this).val() == "" ? 0 : $(this).val();
var val3 = $(this).parent().parent().find(".field3").val() == "" ? 0 : $(this).parent().parent().find(".field3").val();
var rowtotal = parseFloat(!isNaN(val1) ? val1 : 0) + parseFloat(!isNaN(val2) ? val2 : 0) - parseFloat(!isNaN(val3) ? val3 : 0);
$(this).parent().siblings().last().text(rowtotal);
$(this).closest('tr').find('[id*=txtNet]').val(rowtotal);
});
$(".field3").keyup(function () {
var total = 0;
$(".field3").each(function () {
total += parseFloat($(this).val());
});
$(".totaloffield td").eq(3).text(total);
//calculate total for current row
var val1 = $(this).parent().parent().find(".field1").val() == "" ? 0 : $(this).parent().parent().find(".field1").val();
var val2 = $(this).parent().parent().find(".field2").val() == "" ? 0 : $(this).parent().parent().find(".field2").val();
var val3 = $(this).val() == "" ? 0 : $(this).val();
var rowtotal = parseFloat(!isNaN(val1) ? val1 : 0) + parseFloat(!isNaN(val2) ? val2 : 0) - parseFloat(!isNaN(val3) ? val3 : 0);
$(this).parent().siblings().last().text(rowtotal);
$(this).closest('tr').find('[id*=txtNet]').val(rowtotal);
});
})
</script>
Namespaces
C#
using System.Data;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6] { new DataColumn("AdmissionNo", typeof(int)),
new DataColumn("SName", typeof(string)),
new DataColumn("FName",typeof(string)),
new DataColumn("AcademicName",typeof(string)),
new DataColumn("Recievable",typeof(int)),
new DataColumn("RemBal",typeof(int)) });
dt.Rows.Add(1, "John Hammond", "ABC", "2018", 500, 0);
dt.Rows.Add(2, "Mudassar Khan", "xyz", "2018", 500, 0);
dt.Rows.Add(3, "Suzanne Mathews", "abp", "2018", 500, 0);
dt.Rows.Add(4, "Robert Schidner", "ijk", "2018", 500, 0);
gvBudget.DataSource = dt;
gvBudget.DataBind();
}
}
protected void gvBudget_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt1 = (TextBox)e.Row.FindControl("txtReci");
TextBox txt2 = (TextBox)e.Row.FindControl("txtArr");
int add = Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text);
(e.Row.FindControl("txtNet") as TextBox).Text = add.ToString();
}
}
Screenshot