标题:C#中datagridview单元格跳转问题
取消只看楼主
过客而已1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-3-30
结帖率:50%
已结贴  问题点数:10 回复次数:3 
C#中datagridview单元格跳转问题
我现在在keydown事件是写了代码,跳转到当前行的某个单元格,但是运行之后,跳转的是下一行的某个单元格,不知道是为什么?
2012-07-14 16:13
过客而已1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-3-30
得分:0 
程序代码:
 if (this.dgvyzlr.Rows[rowid].Cells["ischild"].Value.Equals("1"))
            {
                //如果是子医嘱,则跳过用法,频率,直接跳到实际用量
                this.dgvyzlr.CurrentCell = this.dgvyzlr.Rows[rowid].Cells["shijiyongliang"];
                this.dgvyzlr.Rows[rowid].Cells["yongfa"].ReadOnly = true;
                this.dgvyzlr.Rows[rowid].Cells["pinlv"].ReadOnly = true;
            }
            else
            {
                if (this.cblrfs.Text.Equals("自由录入"))
                {
                    //规格获得焦点
                    this.dgvyzlr.CurrentCell = this.dgvyzlr.Rows[rowid].Cells["guige"];
                    this.dgvyzlr.Rows[rowid].Cells["guige"].ReadOnly = false;
                }
                else if (this.cblrfs.Text.Equals("临床项目"))
                {
                    //用法获得焦点
                    this.dgvyzlr.CurrentCell = this.dgvyzlr.Rows[rowid].Cells["yongfa"];
                    this.dgvyzlr.Rows[rowid].Cells["yongfa"].ReadOnly = false;
                }
            }
            this.dgvyzlr.Rows[rowid].Cells["yizhuneirong"].Value = this.dgvypxm.Rows[index].Cells["ypmingcheng"].Value.ToString();//医嘱内容
            this.price = double.Parse(this.dgvypxm.Rows[index].Cells["ypdanjia"].Value.ToString());//单价

            this.dgvyzlr.Rows[rowid].Cells["yizhuneirong"].Tag = this.dgvypxm.Rows[index].Cells["ypdanjia"].Value.ToString();
            this.dgvyzlr.Rows[rowid].Cells["guige"].Value = this.dgvypxm.Rows[index].Cells["ypguige"].Value.ToString();//规格
            this.dgvyzlr.Rows[rowid].Cells["yongliangdw"].Value = this.dgvypxm.Rows[index].Cells["ypyongliangdw"].Value.ToString();//用量单位
            this.dgvyzlr.Rows[rowid].Cells["danwei"].Value = this.dgvypxm.Rows[index].Cells["ypdanwei"].Value.ToString();//单位
            this.dgvyzlr.Rows[rowid].Cells["yaopinfenlei"].Value = this.dgvypxm.Rows[index].Cells["ypsx"].Value.ToString();//药品分类
            this.dgvyzlr.Rows[rowid].Cells["ypbh"].Value = this.dgvypxm.Rows[index].Cells["ypbm"].Value.ToString();//药品编号
            this.dgvyzlr.Rows[rowid].Cells["ydbh"].Value = this.dgvypxm.Rows[index].Cells["ypyaodian"].Value.ToString();//药典编号
            jlxs = float.Parse(this.dgvypxm.Rows[index].Cells["jl_xs"].Value.ToString());//剂量
            this.dgvyzlr.Rows[rowid].Cells["lcxmbh"].Value = "000000";
            #region 药房编号
            string yfbh = czyfbh(this.dgvypxm.Rows[0].Cells["ypyaofangmc"].Value.ToString());
            this.dgvyzlr.Rows[rowid].Cells["yfbh"].Value = yfbh;
            #endregion
            heji();
            this.dgvyzlr.BeginEdit(true);
            this.pnypxm.Visible = false;
private void dgvyzlr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            try
            {
                //if (sfzxsjclq)
                //{
                    // 检测是否被编辑单元格
                    if (e.Control is DataGridViewTextBoxEditingControl)
                    {
                        DataGridView dgv = (DataGridView)sender;
                        // 取得被表示的控件
                        DataGridViewTextBoxEditingControl tb = (DataGridViewTextBoxEditingControl)e.Control;
                        // 事件处理器删除
                        tb.PreviewKeyDown -= new PreviewKeyDownEventHandler(dgvyzlr_PreviewKeyDown);
                        // 添加处理事件
                        tb.PreviewKeyDown += new PreviewKeyDownEventHandler(dgvyzlr_PreviewKeyDown);
                        // 事件处理器删除
                        tb.KeyUp -= new KeyEventHandler(dgvyzlr_KeyUp);
                        // 添加处理事件
                        tb.KeyUp += new KeyEventHandler(dgvyzlr_KeyUp);
                    }
                    else if (e.Control is DataGridViewComboBoxEditingControl)
                    {
                        DataGridView dgv = (DataGridView)sender;
                        // 取得被表示的控件
                        DataGridViewComboBoxEditingControl tb = (DataGridViewComboBoxEditingControl)e.Control;
                        // 事件处理器删除
                        tb.PreviewKeyDown -= new PreviewKeyDownEventHandler(dgvyzlr_PreviewKeyDown);
                        // 添加处理事件
                        tb.PreviewKeyDown += new PreviewKeyDownEventHandler(dgvyzlr_PreviewKeyDown);
                    }
                //}
                //else
                //{

                //    sfzxsjclq = true;
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            //if (sfzxsjclq)
            //{
                if (keyData == Keys.Left || keyData == Keys.Right)
                {
                    SendKeys.Send("{F1}");
                    return true;
                }

               
            //}
            //else
            //{
            //    sfzxsjclq = true;
            //}
            return base.ProcessCmdKey(ref msg, keyData);
        }

在dgvyzlr的PreviewKeyDown的事件中调用这个方法的话,执行到this.dgvyzlr.BeginEdit(true)这句,程序就会跳转到EditingControlShowing和ProcessCmdKey里边,然后窗体上就会跳转到下一行,如果不执行这一句,或者是在dgvypxm的keydown事件中执行第一个方法,就没问题,不知道是哪里出了错??请各位帮帮忙
2012-07-15 15:32
过客而已1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-3-30
得分:0 
回复 楼主 过客而已1991
谢谢,我又做了补充,不知可不可以帮我看看,谢谢了
2012-07-15 15:33
过客而已1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-3-30
得分:0 
回复 2楼 peach5460
谢谢,我又做了补充,不知可不可以帮我看看,谢谢了
2012-07-15 15:34



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-373383-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058908 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved