Click here to Skip to main content
15,892,281 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
In my window application datagridview control used .
I need to focus next cell when press enter key in a cell.
Posted
Updated 3-Mar-13 22:35pm
v2
Comments
Sandeep Mewara 1-Mar-13 9:54am    
Tag - ASP.NET, question - winforms. :doh:


Ok, now, what have you tried?

Here, have a look at answer to same question asked earlier: make enter key act as Tab key on Textbox[^] - Solution1 is for Winforms and Solution2 is for Webforms.
 
Share this answer
 
C#
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                int iColumn = dataGridView1.CurrentCell.ColumnIndex;
                int iRow = dataGridView1.CurrentCell.RowIndex;
                if (iColumn == dataGridView1.Columns.Count - 1)
                    dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
                else
                    dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];

            }
        }
 
Share this answer
 
Use this javascript function, it will focus on next input control on pressing enter key.


JavaScript
<script type="text/javascript">
    $(document).ready(function () {

        var inputs = $(':input').keypress(function (e) {
            if (e.which == 13) {
                e.preventDefault();
                var nextInput = inputs.get(inputs.index(this) + 1);
                if (nextInput) {
                    nextInput.focus();
                }
            }
        });

             });
</script>



ASP.NET
<asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox>
<asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown"></asp:textbox>
<asp:textbox id="TextBox3" runat="server" xmlns:asp="#unknown"></asp:textbox>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900