Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi Every one!
i have following code to enter a value in datagridview and press enter to move on next cell

i tried in many way....
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)
               {
                   if (dataGridView1.CurrentCell.ColumnIndex == 2)
                   {
                       dataGridView1.Rows.Add();
                       dataGridView1.CurrentCell = dataGridView1.Rows[iRow + 1].Cells["Name"];
                   }
                   else
                   {

                       dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
                   }

               }
               else
                   dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];

           }
}


this is my code
Posted
Comments
bowlturner 24-Oct-13 10:15am    
is this for .aspx, win forms, wpf? In WPF the tab works for me without having to catch anything.
srigates 24-Oct-13 10:19am    
windows application using c#

1 solution

Try This
C#
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                int col = dataGridView1.CurrentCell.ColumnIndex;
                int row = dataGridView1.CurrentCell.RowIndex;
 
                if (row != dataGridView1.NewRowIndex)
                {
                    if (col == (dataGridView1.Columns.Count - 1))
                    {
                        col = -1;
                        row++;
                    }
                    dataGridView1.CurrentCell = this[col + 1, row];
                }
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }
    }
 
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