在C#winform窗体下
我想在dataGridView1,编辑某单元格后,按enter,比如执行 
if (e.KeyChar == '\r')
{
MessageBox.Show("你好");
}
也就是要监听enter事件。现在是无论我怎么写,按enter读是自动换行到下一行的单元格。
请问:如何实现dataGridView1,编辑某单元格后,按enter,执行enter事件的内容。

解决方案 »

  1.   

    enter的 keyChar不是'\r',
    你设置一个断点,看看是什么,在Keychar enum中好像也有,记不太清了。
      

  2.   

    你说说单击单元格,怎么实现enter监听事件。现在是无论我怎么写,按enter读是自动换行到下一行的单元格。
      

  3.   

    主要是监听不到啊?我要实现的是像textbox里的keypress事件
      

  4.   

    Visual Studio的话,光标移到 if (e.KeyChar == '\r') 这一行,按F9,在最前面会出现一个红点。按F5进行Debug,执行到 if这里就会停下,你可以在 监视窗内 输入e.KeyChar,就可以看到值。这个值当然是你先前按下什么键,就是那个键的值。
      

  5.   

    Gird中没有keypress, 你可以在CellValueChanged事件中编写,当然之前需要多一个判断,是在哪一列
      

  6.   

    http://topic.csdn.net/u/20090610/15/fc7586c1-1441-4aaf-8304-8fb3350fc0ca.html
      

  7.   

    我不是要键的值。是要实现像textbox里的keypress事件。
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
              
                if (e.KeyChar == '\r')
                {
                    MessageBox.Show("你好");
                 }
    要实现这样的功能。但是在dataGridView1里,我选了好多事件,每次按enter都是换到下一行了。
      

  8.   

    用CellLeave事件:
    private void dgvWR_CellLeave(object sender, DataGridViewCellEventArgs e)
            {
                MessageBox.Show(dgvWR.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            }
      

  9.   

    private void dataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
    MessageBox.Show("你好");
    }
      

  10.   

    我要捕获的是enter事件,执行其他代码的,if (e.KeyChar == '\r')主要的是捕获enter见,然后执行下面的代码。MessageBox.Show("你好");是捕获enter后的一个例子。
      

  11.   

    我要的不是dataGridView1,单击时就执行,我要的是dataGridView1的某列里编辑了,然后后按enter,就执行下面的代码。
      

  12.   

    那就用CellValueChanged事件吧,不过在dataGridView加载数据的时候,要做一下判断数据是否全部加载完。。
      

  13.   

    下面是解决的代码。
    protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
            {            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
                {                e.Handled = true;            }
            }        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
            {
                switch (keyData)
                {
                    case System.Windows.Forms.Keys.Enter:                    if (dataGridView1.CurrentCell == dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1])
                            {                            dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4];
                              
                            }
                            else if (dataGridView1.CurrentCell.ColumnIndex == 4)
                            {                            
                                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1 .CurrentCell .RowIndex+1 ].Cells[1];
                                
                            }
                        
                    
                        return true;
                }
                return base.ProcessCmdKey(ref msg, keyData);
            }