标题:C#中datagridview单元格跳转问题
只看楼主
过客而已1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-3-30
结帖率:50%
已结贴  问题点数:10 回复次数:5 
C#中datagridview单元格跳转问题
我现在在keydown事件是写了代码,跳转到当前行的某个单元格,但是运行之后,跳转的是下一行的某个单元格,不知道是为什么?
2012-07-14 16:13
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
得分:5 
那你要贴代码撒...你这么问我怎么回答...

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2012-07-15 07:19
过客而已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
xianfajushi
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:8
帖 子:527
专家分:690
注 册:2007-9-8
得分:5 
下面再举例:
想在dataGridView控件实现点击一个按钮自动把文字填写到选定的单元格内,填写完成后自动跳到下一格,程序集合该怎么写?
1、在如下事件中写要实现的第一个动作程序集合:
private void Button_Click(object sender, EventArgs e)
        {
           dataGridView.Rows[dataGridView.CurrentCellAddress.Y].Cells[dataGridView.CurrentCellAddress.X].Value = “内容";
        }
2、添加要实现的第二个动作程序集合:
private void Button_Click(object sender, EventArgs e)
        {
           dataGridView.Rows[dataGridView.CurrentCellAddress.Y].Cells[dataGridView.CurrentCellAddress.X].Value = “内容";
           //填充完成自动转下一个单元:
           下格 = dataGridView.CurrentCellAddress.X + 1;
           if (下格 < dataGridView.ColumnCount)//限制不超过列数
              dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCellAddress.Y].Cells[下格];
           //如果编辑单元格到最后列自动转下一行的第一个单元格也可以设计,这里不赘述了。
        }
http://blog.
2012-07-20 15:11



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




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

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