如何在datagridview中输入一个值,然后按Enter键移动下一个单元格? [英] how to enter a value in datagridview and press Enter to move on next cell ?

查看:85
本文介绍了如何在datagridview中输入一个值,然后按Enter键移动下一个单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨每一个!

i有以下代码在datagridview中输入一个值并按Enter键继续下一个单元格



i试用很多方式....

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....

 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

推荐答案

试试这个

Try This
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);
        }
    }


这篇关于如何在datagridview中输入一个值,然后按Enter键移动下一个单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆