Hi bkc,
Check this example. Now please take its reference and correct your code.
HTML
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="Valuespanel" runat="server">
<ContentTemplate>
<div>
<asp:GridView ID="gridshipval" runat="server" CssClass="grdCommon" EmptyDataText="NO DATA"
HorizontalAlign="Center" OnRowDataBound="gridshipval_RowDataBound" CellPadding="6"
ForeColor="#333333" GridLines="None" BorderWidth="1" OnSelectedIndexChanged="gridshipval_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BorderStyle="None" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#F0EBEB" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" BorderColor="Maroon" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
function add() {
var grid = document.getElementById('<%=gridshipval.ClientID%>');
var tr = grid.getElementsByTagName("tr");
var sum = 0;
for (var i = 5; i <= 8; i++) {
if (tr[i].getElementsByTagName("td").length > 0) {
var text = tr[i].getElementsByTagName("td")[2].getElementsByTagName('input')[0].value;
if (!isNaN(text) && text != '') {
sum += parseInt(text);
}
}
}
tr[9].getElementsByTagName("td")[2].getElementsByTagName('input')[0].value = sum;
}
</script>
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("Test para"),
new System.Data.DataColumn("unit"),
new System.Data.DataColumn("Textbox") });
dt.Rows.Add("TP0", "%", "");
dt.Rows.Add("TP1", "%", "");
dt.Rows.Add("TP2", "%", "");
dt.Rows.Add("TP3", "%", "");
dt.Rows.Add("TP4", "%", "");
dt.Rows.Add("TP5", "%", "");
dt.Rows.Add("TP6", "%", "");
dt.Rows.Add("TP7", "%", "");
dt.Rows.Add("TP8", "%", "");
dt.Rows.Add("MD", "%", "");
gridshipval.DataSource = dt;
gridshipval.DataBind();
}
}
protected void gridshipval_RowDataBound(object sender, GridViewRowEventArgs e)
{
int pIndx = e.Row.RowIndex;
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtbox = new TextBox();
txtbox.ID = "txtbox" + pIndx;
txtbox.AutoPostBack = false;
txtbox.Width = 75;
txtbox.Attributes.Add("onchange", "add()");
txtbox.CssClass = "allow_decimal";
txtbox.MaxLength = 6;
e.Row.Cells[2].Controls.Add(txtbox);
}
}
protected void gridshipval_SelectedIndexChanged(object sender, EventArgs e)
{
}
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("Test para"), New System.Data.DataColumn("unit"), New System.Data.DataColumn("Textbox")})
dt.Rows.Add("TP0", "%", "")
dt.Rows.Add("TP1", "%", "")
dt.Rows.Add("TP2", "%", "")
dt.Rows.Add("TP3", "%", "")
dt.Rows.Add("TP4", "%", "")
dt.Rows.Add("TP5", "%", "")
dt.Rows.Add("TP6", "%", "")
dt.Rows.Add("TP7", "%", "")
dt.Rows.Add("TP8", "%", "")
dt.Rows.Add("MD", "%", "")
gridshipval.DataSource = dt
gridshipval.DataBind()
End If
End Sub
Protected Sub gridshipval_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim pIndx As Integer = e.Row.RowIndex
If e.Row.RowType = DataControlRowType.DataRow Then
Dim txtbox As TextBox = New TextBox()
txtbox.ID = "txtbox" & pIndx
txtbox.AutoPostBack = False
txtbox.Width = 75
txtbox.Attributes.Add("onchange", "add()")
txtbox.CssClass = "allow_decimal"
txtbox.MaxLength = 6
e.Row.Cells(2).Controls.Add(txtbox)
End If
End Sub
Protected Sub gridshipval_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Screenshot
