加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

DataGridView中,输入数据,按回车,跳到下一列

(2010-02-05 00:18:57)
标签:

杂谈

大家知道,在WinForm开发的系统中,有Header-Detail的结构不少,比如订单、发货单、凭证等等,基本都是Header-Detail结构,而在C#中要开发这样的程序还真有点麻烦,可能是新东西刚出来,要解决这样问题,还真有点烦!同时一般比较好的系统中,你不用Mouse,用键盘就可以操作整个系统。为此,本人专门花了点时间来研究这个问题的解决方案,以和大家共享!

1、首先是换行问题!

 

DataGridView中,你输入数据,按回车,不会跳到下一列去,而是跳到下一行,如果你想通过KeyPress,KeyDown,KeyUp事件达到目的,好像没什么反应。于是我想肯定有其他的方法可以解决。就到微软的网站上去查,果然有相关情况的介绍,但还是达不到我要的,于是改造一下,就OK了。

基本解决方法如下:

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System;

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Collections.Generic;

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Text;

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Windows.Forms;

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifnamespace WindowsApplication1

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    public class CustomDataGridView DataGridView

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    ...{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        protected override bool ProcessDialogKey(Keys keyData)

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        ...{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Keys key (keyData Keys.KeyCode);

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            if (key == Keys.Enter)

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            ...{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                return this.ProcessRightKey(keyData);

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            return base.ProcessDialogKey(keyData);

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        public new bool ProcessRightKey(Keys keyData)

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        ...{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Keys key (keyData Keys.KeyCode);

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            if (key == Keys.Enter)

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            ...{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                //第一种情况:只有一行,且当光标移到最后一列时

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                if ((base.CurrentCell.ColumnIndex == (base.ColumnCount 1)) && (base.RowCount == 1))

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif                ...{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    base.CurrentCell base.Rows[base.RowCount 1].Cells[0];

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    return true;

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif                }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                //第二种情况:有多行,且当光标移到最后一列时,移到下一行第一个单元

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                if ((base.CurrentCell.ColumnIndex == (base.ColumnCount 1)) && (base.CurrentCell.RowIndex (base.RowCount 1)))

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif                ...{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    base.CurrentCell base.Rows[base.CurrentCell.RowIndex 1].Cells[0];

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    return true;

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif                }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                return base.ProcessRightKey(keyData);

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            return base.ProcessRightKey(keyData);

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        protected override bool ProcessDataGridViewKey(KeyEventArgs e)

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        ...{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            if (e.KeyCode == Keys.Enter)

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            ...{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                return this.ProcessRightKey(e.KeyData);

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            //if (e.KeyCode == Keys.F4)

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            //{

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            //    return this.ProcessRightKey(e.KeyData);

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            //}

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            return base.ProcessDataGridViewKey(e);

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif

这样,你输入回车,不会再跳到下一行,而是跳到下一列,只有当你到最后一列时,才会换行。本人试过,基本没有什么Bug,可以拿来做现成的用,大家在写程序时就不需要再写代码了,这个就OK。

那是不是说所有问题都解决了?不是,因为我们在使用的过程中,可能会在DataGridView中使用其他的快捷键,比如微软的标准,按下F4,会弹出下拉列表(如:大家习惯的ComboBox控件就是很好的示例,将光标定位到ComboBox,按下F4,肯定会下拉的,除非自已写来替换掉)。解决这个问题的方法类似!

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有