简体   繁体   中英

pressing enter key and open a new form in datagridview

When a user presses Enter key in one of my datagridview cells (like in column1 cells), a new form like form2 should be opened. I know that in the keypress event I should write e.handled=true; to achieve this, but this code is not working when the datagridview cell is active. How can I do it?

You need to handle the EditingControlShowing event of DataGridView control and PreviewKeyDown event of Cell's Control.

dataGridView1.EditingControlShowing += (senderObject,eventArgs)=>
  {
    eventArgs.Control.PreviewKeyDown += (sa, ea) =>
      {
       if (ea.KeyCode == Keys.Return)
         {
           MessageBox.Show("Something...");
         }
      };
   };

hello try to use this in below create two form on form1 use the grid on given below name or as you can

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            testform tf = new testform();

            {
                if (dataGridView1.CurrentRow.Cells[0].Selected)
                {
                    if (e.KeyCode.ToString() == "F1")

                    {
                        tf.Show();

                    }



                }
            } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM