Hi makumbi,
Please refer below sample.
HTML
<asp:GridView ID="gvStudentMarks" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Account" DataField="Account" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField DataField="Category" HeaderText="Category" />
<asp:BoundField DataField="Stream" HeaderText="Stream" />
<asp:TemplateField HeaderText="Eng">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblEng" runat="server" Text='<%# Eval("Eng") %>' Visible="False"></asp:Label>
<asp:TextBox ID="txtEng" runat="server" Text='<%# Eval("Eng") %>' OnTextChanged="txtEng_TextChanged" AutoPostBack="true"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Maths">
<ItemTemplate>
<asp:TextBox ID="txtMaths" runat="server" Text='<%# Eval("Maths") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TotalMarks">
<ItemTemplate>
<asp:TextBox ID="txtTotalMarks" runat="server" Text='<%# Eval("TotalMarks") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Average">
<ItemTemplate>
<asp:TextBox ID="txtAverage" runat="server" Text='<%# Eval("Average") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Sst" HeaderText="Sst" />
<asp:BoundField DataField="Science" HeaderText="Science" />
<asp:BoundField DataField="Entered Date" HeaderText="Entered Date" />
</Columns>
</asp:GridView>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false">
</asp:ScriptManager>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GridBind();
}
}
private void GridBind()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[11] {new DataColumn("Account"),new DataColumn("Name"),
new DataColumn("Category"),new DataColumn("Stream"),
new DataColumn("Eng"),new DataColumn("Maths"),
new DataColumn("TotalMarks"),new DataColumn("Average"),
new DataColumn("Sst"),new DataColumn("Science"),
new DataColumn("Entered Date")
});
dt.Rows.Add("19-07877", "ONYANGO EMMANUEL ANDREW", "New", "", "60", "61", "121", "", "", "", "07/12/2019 05:02:29 PM");
dt.Rows.Add("19-07869", "KOBUSINGYE NOELA", "New", "", "70", "86", "156", "", "", "", "07/12/2019 05:02:22 PM");
dt.Rows.Add("", "SENTONGO MARIAM", "New", "", "41", "36", "77", "", "", "", "07/12/2019 04:58:38 PM");
dt.Rows.Add("", "NALUBIRA HARRIET", "New", "", "81", "72", "153", "", "", "", "07/12/2019 04:58:23 PM");
dt.Rows.Add("19-07874", "NINYIKIRIZA NTABIRWEKI", "New", "", "34", "39", "73", "", "", "", "07/12/2019 04:51:45 PM");
dt.Rows.Add("19-07875", "NTINWA NAEEM RUYONGA", "New", "", "93", "86", "179", "", "", "", "07/12/2019 04:51:30 PM");
dt.Rows.Add("19-07870", "MPAGI FAISWAL", "New", "", "20", "32", "52", "", "", "", "07/12/2019 04:51:19 PM");
dt.Rows.Add("19-07871", "MUBIRU MUHAMMAD", "New", "", "20", "27", "47", "", "", "", "07/12/2019 04:51:06 PM");
gvStudentMarks.DataSource = dt;
gvStudentMarks.DataBind();
}
protected void txtEng_TextChanged(object sender, EventArgs e)
{
GridViewRow gvRow = (GridViewRow)(sender as TextBox).NamingContainer;
int eng = Convert.ToInt32((gvRow.FindControl("txtEng") as TextBox).Text);
int maths = Convert.ToInt32((gvRow.FindControl("txtMaths") as TextBox).Text);
(gvRow.FindControl("txtAverage") as TextBox).Text = Convert.ToDecimal((eng + maths) / 2.0).ToString("N1");
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.GridBind()
End If
End Sub
Private Sub GridBind()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(10) {New DataColumn("Account"), New DataColumn("Name"),
New DataColumn("Category"), New DataColumn("Stream"),
New DataColumn("Eng"), New DataColumn("Maths"),
New DataColumn("TotalMarks"), New DataColumn("Average"),
New DataColumn("Sst"), New DataColumn("Science"),
New DataColumn("Entered Date")})
dt.Rows.Add("19-07877", "ONYANGO EMMANUEL ANDREW", "New", "", "60", "61", "121", "", "", "", "07/12/2019 05:02:29 PM")
dt.Rows.Add("19-07869", "KOBUSINGYE NOELA", "New", "", "70", "86", "156", "", "", "", "07/12/2019 05:02:22 PM")
dt.Rows.Add("", "SENTONGO MARIAM", "New", "", "41", "36", "77", "", "", "", "07/12/2019 04:58:38 PM")
dt.Rows.Add("", "NALUBIRA HARRIET", "New", "", "81", "72", "153", "", "", "", "07/12/2019 04:58:23 PM")
dt.Rows.Add("19-07874", "NINYIKIRIZA NTABIRWEKI", "New", "", "34", "39", "73", "", "", "", "07/12/2019 04:51:45 PM")
dt.Rows.Add("19-07875", "NTINWA NAEEM RUYONGA", "New", "", "93", "86", "179", "", "", "", "07/12/2019 04:51:30 PM")
dt.Rows.Add("19-07870", "MPAGI FAISWAL", "New", "", "20", "32", "52", "", "", "", "07/12/2019 04:51:19 PM")
dt.Rows.Add("19-07871", "MUBIRU MUHAMMAD", "New", "", "20", "27", "47", "", "", "", "07/12/2019 04:51:06 PM")
gvStudentMarks.DataSource = dt
gvStudentMarks.DataBind()
End Sub
Protected Sub txtEng_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim gvRow As GridViewRow = CType((TryCast(sender, TextBox)).NamingContainer, GridViewRow)
Dim eng As Integer = Convert.ToInt32((TryCast(gvRow.FindControl("txtEng"), TextBox)).Text)
Dim maths As Integer = Convert.ToInt32((TryCast(gvRow.FindControl("txtMaths"), TextBox)).Text)
TryCast(gvRow.FindControl("txtAverage"), TextBox).Text = Convert.ToDecimal((eng + maths) / 2.0).ToString("N1")
End Sub
Screenshot