[英]DataGridView-when I press enter it goes to the next cell
i have a datagridview with 5 columns,when i press "enter" it goes to the next cell and when it arrives to the end of the rows when i press enter it adds a new rows,but my problem is when i move to the previous rows after i press enter it jumps the rows and does not go to the next cells,any help?我有一个包含 5 列的 datagridview,当我按“输入”时它会转到下一个单元格,当我按 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];
}
}
}
}
Forget about CellEnter event and the Form1_KeyPress event also.也忘记 CellEnter 事件和 Form1_KeyPress 事件。 Just handle the dataGridView1_KeyDown event like this:
只需像这样处理dataGridView1_KeyDown事件:
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
int col = dataGridView1.CurrentCell.ColumnIndex;
int row = dataGridView1.CurrentCell.RowIndex;
if (col < dataGridView1.ColumnCount - 1)
{
col ++;
}
else
{
col = 0;
row++;
}
if (row == dataGridView1.RowCount)
dataGridView1.Rows.Add();
dataGridView1.CurrentCell = dataGridView1[col, row];
e.Handled = true;
}
}
Please note that I changed the code a bit, and remember to set the Handled event property to true, otherwise it will process the default behavior.请注意,我稍微更改了代码,并记住将 Handled 事件属性设置为 true,否则它将处理默认行为。
Cheers!干杯!
this works for me这对我有用
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress = true;
int row = dataGridView1.CurrentRow.Index;
int col = dataGridView1.CurrentCell.ColumnIndex;
}
}
Try using this:尝试使用这个:
bool notlastColumn = true;
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
int icolumn = dataGridView1.CurrentCell.ColumnIndex;
int irow = dataGridView1.CurrentCell.RowIndex;
int i = irow;
if (keyData == Keys.Enter)
{
if (icolumn == dataGridView1.Columns.Count - 1)
{
//dataGridView1.Rows.Add();
if (notlastColumn == true)
{
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
}
dataGridView1.CurrentCell = dataGridView1[0, irow + 1];
}
else
{
dataGridView1.CurrentCell = dataGridView1[icolumn + 1, irow];
}
return true;
}
else
if (keyData == Keys.Escape)
{
this.Close();
return true;
}
//below is for escape key return
return base.ProcessCmdKey(ref msg, keyData);
//below is for enter key return
return base.ProcessCmdKey(ref msg, keyData);
}
Just copy and paste the code.只需复制并粘贴代码。
Only thing you should have grid in your form.唯一应该在表单中包含网格的东西。
because the keyDown envent will not fire when press enter on datagridview cell this will be best thanks to @Nagarjun因为当在 datagridview 单元格上按 Enter 键时不会触发 keyDown 环境,这将是最好的感谢@Nagarjun
bool notlastColumn = true;
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg,System.Windows.Forms.Keys keyData)
{
int icolumn = dataGridView1.CurrentCell.ColumnIndex;
int irow = dataGridView1.CurrentCell.RowIndex;
int i = irow;
if (keyData == Keys.Enter)
{
if (icolumn == dataGridView1.Columns.Count - 1)
{
//dataGridView1.Rows.Add();
if (notlastColumn == true)
{
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
}
dataGridView1.CurrentCell = dataGridView1[0, irow + 1];
}
else
{
// to pass hidden cell, because will fire exception
//exception: Current cell cannot be set to an invisible cell
// the do while loop will enable you to pass any hidden cell
do
{
icolumn++;
} while (!dgv[icolumn, irow].Visible);
dataGridView1.CurrentCell = dataGridView1[icolumn, irow];
}
return true;
}
else
if (keyData == Keys.Escape)
{
this.Close();
return true;
}
//below is for escape key return
return base.ProcessCmdKey(ref msg, keyData);
} }
What you can do is handle the KeyDown event to check if the return key is pressed, you cancel the event and tell the application to use the selected index of +1 in your datagridview:您可以做的是处理 KeyDown 事件以检查是否按下了返回键,取消该事件并告诉应用程序在您的 datagridview 中使用选定的 +1 索引:
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
int column = dataGridView1.CurrentCell.ColumnIndex;
int row = dataGridView1.CurrentCell.RowIndex;
dataGridView1.CurrentCell = dataGridView1[column, row+1];
e.Handled=true;
}
}
To prevent problems you should also insert a clause to check if there are items available after the one the user pressed return.为了防止出现问题,您还应该插入一个子句来检查用户按下回车键后是否有可用的项目。
Using this solution:使用此解决方案:
private void Form1_Load(object sender, EventArgs e)
{
dtg.AllowUserToAddRows = false;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
int iCol = dtg.CurrentCell.ColumnIndex;
int iRow = dtg.CurrentCell.RowIndex;
if (keyData == Keys.Enter)
{
if (iCol == dtg.ColumnCount - 1)
{
if (iRow + 1 == dtg.RowCount)
{
dtg.Rows.Add();
}
dtg.CurrentCell = dtg[0, iRow + 1];
}
else
{
dtg.CurrentCell = dtg[iCol + 1, iRow];
}
return true;
}
else if (keyData == Keys.Escape)
{
Close();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Good Luck ...祝你好运 ...
Just move to right when key press is enter...只需在输入按键时向右移动...
private void datagridview_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{Right}");
}
}
This code worked well for me这段代码对我很有效
bool notlastColumn = true;
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
int icolumn = dgvReceDet.CurrentCell.ColumnIndex;
int irow = dgvReceDet.CurrentCell.RowIndex;
int i = irow;
if (keyData == Keys.Enter)
{
if (icolumn == dgvReceDet.Columns.Count - 1)
{
//dataGridView1.Rows.Add();
if (notlastColumn == true)
{
dgvReceDet.CurrentCell = dgvReceDet.Rows[i].Cells[0];
}
dgvReceDet.CurrentCell = dgvReceDet[0, irow + 1];
}
else
{
// to pass hidden cell, because will fire exception
//exception: Current cell cannot be set to an invisible cell
// the do while loop will enable you to pass any hidden cell
do
{
icolumn++;
}
while (icolumn <= dgvReceDet.Columns.Count - 1 && !dgvReceDet[icolumn, irow].Visible);
if (icolumn <= dgvReceDet.Columns.Count - 1)
{
dgvReceDet.CurrentCell = dgvReceDet[icolumn, irow];
}
else
{
SendKeys.Send("{DOWN}");
SendKeys.Send("{HOME}");
}
}
return true;
}
else
if (keyData == Keys.Escape)
{
this.Close();
return true;
}
//below is for escape key return
return base.ProcessCmdKey(ref msg, keyData);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.