dataGridView已经绑定好了,键盘点击“Enter”按钮后跳到另外的界面,如何编写后台代码呢

解决方案 »

  1.   


    private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
            {
                //判斷按下的鍵是否為Enter鍵
                if (e.KeyValue == Keys.Enter)
                {
                    //編寫業務代碼 
                    MessageBox.Show("You press the enter key");
                }
            }不知道事件找对没,你自己试试吧
      

  2.   

    父窗体 点击父窗体上的值 之后点击 enter 之后调到 子窗体
    namespace Datagridv
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public string selecvalue = null;
            private void Form1_Load(object sender, EventArgs e)
            {
                List<Data> List = new List<Data>();
                Data d = null;
                for (int i = 0; i < 5; i++)
                {
                    d = new Data();
                    d.Str_id = i.ToString();
                    d.Str_id1 = i.ToString();
                    d.Str_id2 = i.ToString();
                    d.Str_id3 = i.ToString();
                    d.Str_id4 = i.ToString();
                    d.Str_id5 = i.ToString();
                    List.Add(d);
                }
                dataGridView1.DataSource = List;
            }        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
            {            int a = e.KeyValue;
                // 值13 就代表 enter 键 
                if (a==13)
                {
                    Form2 fr = new Form2(selecvalue);
                    fr.ShowDialog();
                }
            }        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                selecvalue = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            }
        }
        public class Data 
        {
            private string str_id;        public string Str_id
            {
                get { return str_id; }
                set { str_id = value; }
            }
            private string str_id1;        public string Str_id1
            {
                get { return str_id1; }
                set { str_id1 = value; }
            }
            private string str_id2;        public string Str_id2
            {
                get { return str_id2; }
                set { str_id2 = value; }
            }
            private string str_id3;        public string Str_id3
            {
                get { return str_id3; }
                set { str_id3 = value; }
            }
            private string str_id4;        public string Str_id4
            {
                get { return str_id4; }
                set { str_id4 = value; }
            }
            private string str_id5;        public string Str_id5
            {
                get { return str_id5; }
                set { str_id5 = value; }
            }
        }
    }
    子窗体采用构造函数来传参
    namespace Datagridv
    {
        public partial class Form2 : Form
        {
            string a = "";
            public Form2(string ster)
            {
                InitializeComponent();
                a = ster;
            }        private void Form2_Load(object sender, EventArgs e)
            {
                label1.Text = a.ToString();
            }
        }
    }
      

  3.   


    private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
            {
                 if (e.KeyValue == Keys.Enter)
                {
                   return base.ProcessDialogKey(Keys.Tab);
                }
            }
      

  4.   

    改一下 private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
            {
                //判斷按下的鍵是否為Enter鍵
                if (e.KeyChar == (char)(13))
                {
                    //編寫業務代碼 
                    MessageBox.Show("You press the enter key");
                }
            }