Hi makumbi,
Please refer below updated code.
HTML
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="TeachersCommentsGrd" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" Enabled="False" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="admno">
<ItemTemplate>
<asp:TextBox ID="Admno" runat="server" Text='<%# Eval("admno") %>' ReadOnly="True"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="Names" runat="server" Text='<%# Eval("name") %>' ReadOnly="True"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Grade">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Grade") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Grade">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Grade") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Grade") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comments">
<ItemTemplate>
<asp:TextBox ID="TeacherComments" runat="server"
OnTextChanged="TeacherComments_TextChanged" Text='<%# Eval("Remark") %>'
CssClass="TeacherComments" onkeyup="enter(this)" AutoPostBack="True"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HiddenField ID="hfRowIndex" runat="server" />
<asp:HiddenField ID="hfCellIndex" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
function enter(obj) {
var tr = obj.parentNode.parentNode;
var cellIndex = obj.parentNode.cellIndex;
var rowIndex = obj.parentNode.parentNode.rowIndex;
if (event.keyCode == 37) //Left
{
if (cellIndex > 0) {
//tr.parentNode.rows[rowIndex - 1].cells[cellIndex - 1].getElementsByTagName('INPUT')[0].focus();
}
}
if (event.keyCode == 38) //Up
{
if (rowIndex > 1) {
tr.parentNode.rows[rowIndex - 1].cells[cellIndex].getElementsByTagName('INPUT')[0].focus();
}
}
if (event.keyCode == 39) //Right
{
if (cellIndex <= tr.cells.length - 1) {
tr.parentNode.rows[rowIndex + 1].cells[cellIndex].getElementsByTagName('INPUT')[0].focus();
}
}
if (event.keyCode == 40) //Down
{
if (rowIndex < tr.parentNode.rows.length - 1) {
tr.parentNode.rows[rowIndex + 1].cells[cellIndex].getElementsByTagName('INPUT')[0].focus();
}
}
};
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
prm.add_endRequest(function (sender, e) {
if (sender._postBackSettings.panelsToUpdate != null) {
var rowIndex = parseInt(document.getElementById("<%=hfRowIndex.ClientID%>").value);
var row = document.getElementById("<%=TeachersCommentsGrd.ClientID%>").rows[rowIndex + 1];
if (typeof (row) != "undefined") {
var celIndex = parseInt(document.getElementById("<%=hfCellIndex.ClientID%>").value);
var checkbox = row.cells[0].getElementsByTagName("INPUT");
if (!checkbox[0].checked) {
checkbox[0].checked = true;
}
var controls = row.cells[celIndex].getElementsByTagName("INPUT");
for (var i = 0; i < controls.length; i++) {
switch (controls[i].className) {
case "TeacherComments":
controls[i].focus();
return;
default:
}
}
}
}
});
}
</script>
</asp:Content>
Code
CS
protected void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("admno");
dt.Columns.Add("name");
dt.Columns.Add("Grade");
dt.Columns.Add("Remark");
dt.Rows.Add("1", "John", "A", "Good1");
dt.Rows.Add("2", "Suzen", "A+", "Good2");
dt.Rows.Add("1", "JohnHH", "AB", "Good3");
dt.Rows.Add("2", "SuzeEn", "B+", "Good4");
TeachersCommentsGrd.DataSource = dt;
TeachersCommentsGrd.DataBind();
}
}
protected void TeacherComments_TextChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as TextBox).NamingContainer as GridViewRow;
int kk = row.RowIndex;
hfRowIndex.Value = row.RowIndex.ToString();
hfCellIndex.Value = "5";
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add("admno")
dt.Columns.Add("name")
dt.Columns.Add("Grade")
dt.Columns.Add("Remark")
dt.Rows.Add("1", "John", "A", "Good1")
dt.Rows.Add("2", "Suzen", "A+", "Good2")
dt.Rows.Add("1", "JohnHH", "AB", "Good3")
dt.Rows.Add("2", "SuzeEn", "B+", "Good4")
TeachersCommentsGrd.DataSource = dt
TeachersCommentsGrd.DataBind()
End If
End Sub
Protected Sub TeacherComments_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, TextBox)).NamingContainer, GridViewRow)
Dim kk As Integer = row.RowIndex
hfRowIndex.Value = row.RowIndex.ToString()
hfCellIndex.Value = "5"
End Sub
Screenshot