当前位置:编程学习 > C#/ASP.NET >>

C# DataGridView 编辑状态下回车跳转问题

基本内容如下:
我用2个方法实现DataGridView按回车使某单元格获得焦点,ProcessCmdKey是重写方法,可以在未编辑状态实现跳转,但下面DataGridView的CellValueChanged事件就不可以实现编辑状态下的跳转,当用一下方法实现时,Col<3时,编辑状态输入回车总会条到下一行Col+1位置,不知为什么,但Col=3时可以是下一行首列获得焦点,希望各位达人帮忙指教。
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (this.ActiveControl == dataGridView1)
            {
                if (keyData == Keys.Enter)
                {
                    if (dataGridView1.CurrentCell.ColumnIndex == 3)
                    {
                        if (dataGridView1.CurrentCell.RowIndex == dataGridView1.RowCount-1)
                        {
                            dataGridView1.Rows.Add(1);
                        }
                        dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex + 1].Cells[0];
                        return true;
                    }
                    else
                    {
                        System.Windows.Forms.SendKeys.Send("{tab}");
                        return true;
                    }
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int m = e.RowIndex;
            int n = e.ColumnIndex;
            if (n >= 4 || m < 0)
            {
                return;
            }
            if (n < 3)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[m].Cells[n + 1];
                //这里无论设置m还是m-1或者m+1时都会跳到下一行,十分不解。
            }
            if (n == 3)
            {
                if (m == dataGridView1.RowCount - 1)
                {
                    dataGridView1.Rows.Add(1);
                }
                dataGridView1.CurrentCell = dataGridView1.Rows[m+1].Cells[0];
            }
        } --------------------编程问答-------------------- 我也想知道,这个问题困了我许久
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,