问题描述
当我按下Enter键时,datagrid转到下一行,然后更改我想要的当前单元格.
但是我想在按Enter键时需要它,而无需直接进入下一行即可直接获取我想要的当前单元格.
我想知道如何忽略输入的keypress事件.
when i pressed enter key , datagrid get to next row and then change current cell i want.
but i want to need that when i pressed enter key ,want to get directly current cell that i want without going next row .
i want to know how i can ingnore enter keypress event .
推荐答案
char EnterKey = (char)Keys.Enter;
private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == EnterKey)
{
// here''s where you do whatever it is you want to do
e.Handled = true; // does not work as expected
}
}
相反,您将做了这样的变通方法:
Instead you are going to have do some work-around like this:
char EnterKey = (char)Keys.Enter;
DataGridViewCell currentCell;
private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == EnterKey)
{
// here''s where you do whatever it is you want to do
e.Handled = true; // does not work as expected
dataGridView1.CurrentCell = currentCell;
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
currentCell = dataGridView1.CurrentCell;
}
但是,在您的问题中:按Enter键时您真正想要发生的事情对我来说还不清楚.
为什么当按下Enter键时,为什么希望所选单元格在同一行上保持不变?
应该有很好的理由,有特殊的理由,要超越标准控件的预期默认行为.
But, in your question: what you actually want to happen when you press Enter is not clear to me.
Why do you want the selected cell to remain the same, on the same row, when the Enter key is pressed ?
There should be very good reasons, special reasons, for over-riding the expected default behaviors of standard controls.
这篇关于要求在datagrid视图中输入keypress事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!