HTML:
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />
<asp:TemplateField HeaderText="Prices">
<ItemTemplate>
<asp:TextBox ID="txtPrice" AutoPostBack="true" OnTextChanged="Price_TextChanged"
Text='<%# Eval("Price") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
Total Price:
<asp:Label ID="lblTotalPrice" runat="server"></asp:Label>
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("ProductName", typeof(string )),
new DataColumn("Price", typeof(int)) });
dt.Rows.Add("Stumps", 400);
dt.Rows.Add("Bat", 500);
dt.Rows.Add("Ball", 50);
this.gvProduct.DataSource = dt;
this.gvProduct.DataBind();
}
}
protected void Price_TextChanged(object sender, EventArgs e)
{
GridViewRow row2 = (sender as TextBox).NamingContainer as GridViewRow;
int price = 0;
foreach (GridViewRow row in gvProduct.Rows)
{
price += Convert.ToInt32(((TextBox)(row.Cells[1].FindControl("txtPrice"))).Text);
}
row2.Cells[1].FindControl("txtPrice").Focus();
this.lblTotalPrice.Text = price.ToString();
}