Hi makumbi,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="MarksGridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField AccessibleHeaderText="Admno" DataField="admno" HeaderText="Admno" />
<asp:BoundField DataField="Names" HeaderText="Names" />
<asp:TemplateField AccessibleHeaderText="Score" HeaderText="Score">
<ItemTemplate>
<asp:TextBox ID="txtScore" runat="server" Text='<%# Bind("Score") %>'
AutoPostBack="true" OnTextChanged="OnScoreChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Agg" HeaderText="Aggregate">
<ItemTemplate>
<asp:TextBox ID="txtAgg" runat="server" Text='<%# Bind("agg") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks">
<ItemTemplate>
<asp:TextBox ID="txtRemarks" runat="server" Text='<%# Bind("Remarks") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks">
<ItemTemplate>
<asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
MarksGridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
MarksGridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnScoreChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
TextBox txtAgg = row.FindControl("txtAgg") as TextBox;
TextBox txtRemarks = row.FindControl("txtRemarks") as TextBox;
txtAgg.Text = enggrade(Convert.ToDouble((sender as TextBox).Text));
txtRemarks.Text = hmcomment((sender as TextBox).Text);
}
protected void OnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string txtAgg = (row.FindControl("txtAgg") as TextBox).Text;
string txtRemarks = (row.FindControl("txtRemarks") as TextBox).Text;
}
protected void BindGrid()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[]
{
new System.Data.DataColumn("Admno"),
new System.Data.DataColumn("Names"),
new System.Data.DataColumn("Score"),
new System.Data.DataColumn("agg"),
new System.Data.DataColumn("Remarks")
});
dt.Rows.Add(1, "TAMALE RICHARD", 99, 1, "Very good!Keep it up.");
dt.Rows.Add(2, "TENDO ALPHA", 77, 3, "Works with interest.");
dt.Rows.Add(3, "WABULA PHILLIP", 66, 4, "Good!");
MarksGridView1.DataSource = dt;
MarksGridView1.DataBind();
}
public string hmcomment(string tt)
{
string comment = "";
if (string.IsNullOrEmpty(tt) || Convert.ToInt32(tt) == -1)
{
comment = "";
}
if (Convert.ToInt32(tt) > 90)
{
comment = "Very good! Keep it up.";
}
switch (Convert.ToInt32(tt))
{
case 20:
comment = "You have to read hard for better results.";
break;
case 21:
comment = "You need more consultation with teachers.";
break;
case 22:
comment = "You should work hard in order to improve on your scores.";
break;
case 23:
comment = "A lot of effort is needed for better results.";
break;
case 24:
comment = "You have to work extra hard.";
break;
case 25:
comment = "Do a lot of revision for a better performance.";
break;
case 26:
comment = "Try harder next time.";
break;
case 27:
comment = "Needs to work very hard.";
break;
case 28:
comment = "Below required grade; you should double your efforts.";
break;
case 29:
comment = "Work harder for better grades.";
break;
case 30:
comment = "Weak results; you shouldn't give up. ";
break;
case 31:
comment = "Consult your teachers where you don't understand.";
break;
case 32:
comment = "You should pay more attention.";
break;
case 33:
comment = "Continue trying in order to catch up.";
break;
case 34:
comment = "Double your effort for better scores.";
break;
case 35:
comment = "You should do a lot of practice for better performance.";
break;
case 36:
comment = "You should pay more attention.";
break;
case 37:
comment = "More seriousness needed.";
break;
case 38:
comment = "You should put in more effort for a better grade.";
break;
case 39:
comment = "You should pay more attention during lessons.";
break;
case 40:
case 41:
comment = "More concentration needed.";
break;
case 42:
comment = "Put in more effort.";
break;
case 43:
comment = "Regular exercises can help you to improve.";
break;
case 44:
comment = "Read harder next time.";
break;
case 45:
comment = "Do More serious reading.";
break;
case 46:
comment = "Intensify your reading and concentrate.";
break;
case 47:
comment = "Just below average; a lot of effort needed.";
break;
case 48:
comment = "There is room for improvement, consult your teachers.";
break;
case 49:
comment = "Intensive reading needed.";
break;
case 50:
comment = "A lot more effort needed; don't despair.";
break;
case 51:
comment = "Just average; aim higher.";
break;
case 52:
comment = "A little bit of more reading needed.";
break;
case 53:
comment = "Average performance; strive harder.";
break;
case 54:
comment = "This is a good start; don't relax.";
break;
case 55:
comment = "Fair! You can even score more !.";
break;
case 56:
comment = "Fairly good; don't relax.";
break;
case 57:
comment = "Good! You should aim at higher scores.";
break;
case 58:
comment = "Fair; aim higher.";
break;
case 59:
comment = "Fairly good! You can do better.";
break;
case 60:
comment = "Fair! More concentration required.";
break;
case 61:
comment = "Just slightly above average; read harder.";
break;
case 62:
comment = "Do more serious revision.";
break;
case 63:
comment = "You can do better with more concentration.";
break;
case 64:
comment = "You should aim higher.";
break;
case 65:
comment = "You can do better with more effort.";
break;
case 66:
comment = "Good!";
break;
case 67:
comment = "Capable of better performance.";
break;
case 68:
comment = "Fair mark, but you should aim higher.";
break;
case 69:
comment = "Good! You can do better.";
break;
case 70:
comment = "Quite good.";
break;
case 71:
comment = "Good effort.";
break;
case 72:
comment = "Capable of doing well.";
break;
case 73:
comment = "Q.Good! Keep it up.";
break;
case 74:
comment = "Good! You should aim at the best.";
break;
case 75:
comment = "Good work, keep it up.";
break;
case 76:
comment = "Quite good.";
break;
case 77:
comment = "Works with interest.";
break;
case 78:
comment = "Quite good, should still aim higher.";
break;
case 79:
comment = "Good, Strive harder.";
break;
case 80:
comment = "Quite good! You can still do better.";
break;
case 81:
comment = "Good work! should still aim higher.";
break;
case 82:
comment = "Good! You can do better";
break;
case 83:
comment = "Quite good.";
break;
case 84:
comment = "Good! You should still aim higher.";
break;
case 85:
comment = "Quite good work, but there is still room for improvement.";
break;
case 86:
case 87:
case 88:
case 89:
case 90:
comment = "Good,but aim higher.";
break;
}
return comment;
}
public string enggrade(double value)
{
string grade = "";
if (95 <= value && value <= 100)
{
grade = "1";
}
else if (80 <= value && value <= 94)
{
grade = "2";
}
else if (75 <= value && value <= 79)
{
grade = "3";
}
else if (70 <= value && value <= 74)
{
grade = "4";
}
else if (65 <= value && value <= 69)
{
grade = "5";
}
else if (60 <= value && value <= 64)
{
grade = "6";
}
else if (50 <= value && value <= 59)
{
grade = "7";
}
else if (45 <= value && value <= 49)
{
grade = "8";
}
else if (0 <= value && value <= 44)
{
grade = "9";
}
return grade;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Protected Sub OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
MarksGridView1.EditIndex = e.NewEditIndex
Me.BindGrid()
End Sub
Protected Sub OnRowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
MarksGridView1.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub OnScoreChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast(TryCast(sender, TextBox).NamingContainer, GridViewRow)
Dim txtAgg As TextBox = TryCast(row.FindControl("txtAgg"), TextBox)
Dim txtRemarks As TextBox = TryCast(row.FindControl("txtRemarks"), TextBox)
txtAgg.Text = enggrade(Convert.ToDouble(TryCast(sender, TextBox).Text))
txtRemarks.Text = hmcomment(TryCast(sender, TextBox).Text)
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, LinkButton)).NamingContainer, GridViewRow)
Dim txtAgg As String = TryCast(row.FindControl("txtAgg"), TextBox).Text
Dim txtRemarks As String = TryCast(row.FindControl("txtRemarks"), TextBox).Text
End Sub
Protected Sub BindGrid()
Dim dt As Data.DataTable = New Data.DataTable()
dt.Columns.AddRange(New Data.DataColumn() {
New Data.DataColumn("Admno"),
New Data.DataColumn("Names"),
New Data.DataColumn("Score"),
New Data.DataColumn("agg"),
New Data.DataColumn("Remarks")})
dt.Rows.Add(1, "TAMALE RICHARD", 99, 1, "Very good!Keep it up.")
dt.Rows.Add(2, "TENDO ALPHA", 77, 3, "Works with interest.")
dt.Rows.Add(3, "WABULA PHILLIP", 66, 4, "Good!")
MarksGridView1.DataSource = dt
MarksGridView1.DataBind()
End Sub
Public Function hmcomment(ByVal tt As String) As String
Dim comment As String = ""
If String.IsNullOrEmpty(tt) OrElse Convert.ToInt32(tt) = -1 Then
comment = ""
End If
If Convert.ToInt32(tt) > 90 Then
comment = "Very good! Keep it up."
End If
Select Case Convert.ToInt32(tt)
Case 20
comment = "You have to read hard for better results."
Case 21
comment = "You need more consultation with teachers."
Case 22
comment = "You should work hard in order to improve on your scores."
Case 23
comment = "A lot of effort is needed for better results."
Case 24
comment = "You have to work extra hard."
Case 25
comment = "Do a lot of revision for a better performance."
Case 26
comment = "Try harder next time."
Case 27
comment = "Needs to work very hard."
Case 28
comment = "Below required grade; you should double your efforts."
Case 29
comment = "Work harder for better grades."
Case 30
comment = "Weak results; you shouldn't give up."
Case 31
comment = "Consult your teachers where you don't understand."
Case 32
comment = "You should pay more attention."
Case 33
comment = "Continue trying in order to catch up."
Case 34
comment = "Double your effort for better scores."
Case 35
comment = "You should do a lot of practice for better performance."
Case 36
comment = "You should pay more attention."
Case 37
comment = "More seriousness needed."
Case 38
comment = "You should put in more effort for a better grade."
Case 39
comment = "You should pay more attention during lessons."
Case 40, 41
comment = "More concentration needed."
Case 42
comment = "Put in more effort."
Case 43
comment = "Regular exercises can help you to improve."
Case 44
comment = "Read harder next time."
Case 45
comment = "Do More serious reading."
Case 46
comment = "Intensify your reading and concentrate."
Case 47
comment = "Just below average; a lot of effort needed."
Case 48
comment = "There is room for improvement, consult your teachers."
Case 49
comment = "Intensive reading needed."
Case 50
comment = "A lot more effort needed; don't despair."
Case 51
comment = "Just average; aim higher."
Case 52
comment = "A little bit of more reading needed."
Case 53
comment = "Average performance; strive harder."
Case 54
comment = "This is a good start; don't relax."
Case 55
comment = "Fair! You can even score more !."
Case 56
comment = "Fairly good; don't relax."
Case 57
comment = "Good! You should aim at higher scores."
Case 58
comment = "Fair; aim higher."
Case 59
comment = "Fairly good! You can do better."
Case 60
comment = "Fair! More concentration required."
Case 61
comment = "Just slightly above average; read harder."
Case 62
comment = "Do more serious revision."
Case 63
comment = "You can do better with more concentration."
Case 64
comment = "You should aim higher."
Case 65
comment = "You can do better with more effort."
Case 66
comment = "Good!"
Case 67
comment = "Capable of better performance."
Case 68
comment = "Fair mark, but you should aim higher."
Case 69
comment = "Good! You can do better."
Case 70
comment = "Quite good."
Case 71
comment = "Good effort."
Case 72
comment = "Capable of doing well."
Case 73
comment = "Q.Good! Keep it up."
Case 74
comment = "Good! You should aim at the best."
Case 75
comment = "Good work, keep it up."
Case 76
comment = "Quite good."
Case 77
comment = "Works with interest."
Case 78
comment = "Quite good, should still aim higher."
Case 79
comment = "Good, Strive harder."
Case 80
comment = "Quite good! You can still do better."
Case 81
comment = "Good work! should still aim higher."
Case 82
comment = "Good! You can do better"
Case 83
comment = "Quite good."
Case 84
comment = "Good! You should still aim higher."
Case 85
comment = "Quite good work, but there is still room for improvement."
Case 86, 87, 88, 89, 90
comment = "Good,but aim higher."
End Select
Return comment
End Function
Public Function enggrade(ByVal value As Double) As String
Dim grade As String = ""
If 95 <= value AndAlso value <= 100 Then
grade = "1"
ElseIf 80 <= value AndAlso value <= 94 Then
grade = "2"
ElseIf 75 <= value AndAlso value <= 79 Then
grade = "3"
ElseIf 70 <= value AndAlso value <= 74 Then
grade = "4"
ElseIf 65 <= value AndAlso value <= 69 Then
grade = "5"
ElseIf 60 <= value AndAlso value <= 64 Then
grade = "6"
ElseIf 50 <= value AndAlso value <= 59 Then
grade = "7"
ElseIf 45 <= value AndAlso value <= 49 Then
grade = "8"
ElseIf 0 <= value AndAlso value <= 44 Then
grade = "9"
End If
Return grade
End Function
Screenshot