Hi makumbi,
Please refer below sample.
HTML
<asp:GridView ID="gvStudentMarks" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvStudentMarks_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Formno." DataField="Formno." />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField DataField="Category" HeaderText="Category" />
<asp:BoundField DataField="Stream" HeaderText="Stream" />
<asp:TemplateField HeaderText="Eng">
<ItemTemplate>
<asp:Label ID="lblEng" runat="server" Text='<%# Eval("Eng") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Maths">
<ItemTemplate>
<asp:Label ID="lblMaths" runat="server" Text='<%# Eval("Maths") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TotalMarks">
<ItemTemplate>
<asp:Label ID="lblTotalMarks" runat="server" Text='<%# Eval("TotalMarks") %>'></asp:Label>
</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>
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("Formno."),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 gvStudentMarks_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int eng = Convert.ToInt32((e.Row.FindControl("lblEng") as Label).Text);
int maths = Convert.ToInt32((e.Row.FindControl("lblMaths") as Label).Text);
(e.Row.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("Formno."), 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 gvStudentMarks_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim eng As Integer = Convert.ToInt32(TryCast(e.Row.FindControl("lblEng"), Label).Text)
Dim maths As Integer = Convert.ToInt32(TryCast(e.Row.FindControl("lblMaths"), Label).Text)
TryCast(e.Row.FindControl("txtAverage"), TextBox).Text = Convert.ToDecimal((eng + maths) / 2.0).ToString("N1")
End If
End Sub
Screenshot