【发布时间】:2012-03-12 20:10:51
【问题描述】:
我有一个 5 列的 datagridview,当我按“enter”时,它会转到下一个单元格,当我按 enter 时它到达行尾时,它会添加一个新行,但我的问题是当我移动时按回车键后到前一行,它会跳转行并且不会转到下一个单元格,有什么帮助吗?
public partial class Form1 : Form
{
public static int Col;
public static int Row;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Rows.Add();
}
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
Col = dataGridView1.CurrentCellAddress.X;
Row = dataGridView1.CurrentCellAddress.Y;
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (int)Keys.Enter)
{
if (Col + 1 < 5)
{
dataGridView1.CurrentCell = dataGridView1[Col + 1, Row];
}
else
{
dataGridView1.Rows.Add();
dataGridView1.CurrentCell = dataGridView1[Col - 4, Row + 1];
}
}
}
}
【问题讨论】: