Please check this
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridcalc.aspx.cs" Inherits="WebApplication1.gridcalc" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="Gridview1" runat="server" EmptyDataText="Data is not available" AutoGenerateColumns="false" OnRowDataBound="Gridview1_RowDataBound">
<columns>
<asp:TemplateField HeaderText="ID">
<itemtemplate>
<asp:TextBox ID="txtID" runat="server" Text='<%#Eval("ID") %>' />
<asp:TemplateField HeaderText="Quantity">
<itemtemplate>
<asp:TextBox ID="txtQuantity" runat="server" AutoPostBack="True" Text='<%#Eval("Quantity") %>' OnTextChanged="txtQuantity_TextChanged" />
<asp:TemplateField HeaderText="Rate">
<itemtemplate>
<asp:TextBox ID="txtRate" runat="server" AutoPostBack="True" Text='<%#Eval("Rate") %>' OnTextChanged="txtRate_TextChanged" />
<asp:TemplateField HeaderText="Total">
<itemtemplate>
<asp:TextBox ID="txtTotal" runat="server" Text='<%#Eval("Total") %>' />
</form>
</body>
</html>
Code file:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class gridcalc : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dtPart = new DataTable();
dtPart.Columns.Add("ID", typeof(int));
dtPart.Columns.Add("Quantity", typeof(int));
dtPart.Columns.Add("Rate", typeof(int));
dtPart.Columns.Add("Total", typeof(int));
dtPart.Rows.Add(25, 1, 2, 3);
dtPart.Rows.Add(50, 4, 5, 6);
Gridview1.DataSource = dtPart;
Gridview1.DataBind();
}
}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtRate = (TextBox)e.Row.Cells[1].FindControl("txtRate");
TextBox txtQuantity = (TextBox)e.Row.Cells[2].FindControl("txtQuantity");
TextBox txtTotal = (TextBox)e.Row.Cells[3].FindControl("txtTotal");
int T = 0;
var R = int.Parse(txtRate.Text);
var Q = int.Parse(txtQuantity.Text);
T += Convert.ToInt32(R * Q);
txtTotal.Text = T.ToString();
}
}
protected void txtQuantity_TextChanged(object sender, EventArgs e)
{
TextBox qtxt = (TextBox)sender;
GridViewRow gvRow = (GridViewRow)qtxt.Parent.Parent;
TextBox Rate = (TextBox)gvRow.FindControl("txtRate");
TextBox Total = (TextBox)gvRow.FindControl("txtTotal");
try
{
Total.Text = ((Convert.ToInt32(qtxt.Text)) * (Convert.ToInt32(Rate.Text))).ToString();
}
catch { Total.Text = "0"; qtxt.Text = "0"; }
}
protected void txtRate_TextChanged(object sender, EventArgs e)
{
TextBox Rate = (TextBox)sender;
GridViewRow gvRow = (GridViewRow)Rate.Parent.Parent;
TextBox qtxt = (TextBox)gvRow.FindControl("txtQuantity");
TextBox Total = (TextBox)gvRow.FindControl("txtTotal");
try
{
Total.Text = ((Convert.ToInt32(qtxt.Text)) * (Convert.ToInt32(Rate.Text))).ToString();
}
catch { Total.Text = "0"; qtxt.Text = "0"; }
}
}
}