问题的提出
经常使用的datagridview控件,当你利用它来输入数据时,如果你按下Enter 时,系统默认的是将输入焦点移动至当前列的下一行,但按中国人日常的习惯,我们希望它从左到右进行输入,怎么办呢?
这里我们就要修改系统的两个默认键盘时间,以实现我们自如的控制datagridview埪件对回车Press事件的处理方式,具体怎么操作呢?
新建一用户控件-该用户埪件继承于datagridview,在该埪件的代码中重写ProcessDialogKey, ProcessDataGridViewKey两个事件,让将该用户控件添加到你的项目中就可以应用了。
Code
1 public partial class MyDataGridView : DataGridView
2 {
3 private string _startEditColumnName = "";
4 /// <summary>
5 /// 开始编辑列的名称
6 /// </summary>
7 public string StartEditColumnName
8 {
9 get { return _startEditColumnName; }
10 set { _startEditColumnName = value; }
11 }
12 private LJFWCommon.ConstEnum.OperateStateEnum _operateState = LJFWCommon.ConstEnum.OperateStateEnum.New;
13 /// <summary>
14 /// 操作状态
15 /// </summary>
16 public LJFWCommon.ConstEnum.OperateStateEnum OperateState
17 {
18 get { return _operateState; }
19 set { _operateState = value; }
20 }
21
22 public MyDataGridView()
23 {
24 InitializeComponent();
25 }
26
27 public MyDataGridView(IContainer container)
28 {
29 container.Add(this);
30
31 InitializeComponent();
32 }
33
34 protected override void OnScroll(ScrollEventArgs e)
35 {
36 //this.Invalidate();
37 base.OnScroll(e);
38 }
39
40 protected override void OnRowEnter(DataGridViewCellEventArgs e)
41 {
42 //this.Invalidate();
43
44 base.OnRowEnter(e);
45 }
46
47 protected override void OnPaint(PaintEventArgs e)
48 {
49 base.OnPaint(e);
50 }
51
52 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
53 {
54 //if (keyData == Keys.Enter)
55 //{
56 // SendKeys.Send("{tab}");
57 // return true;
58 //}
59
60 return base.ProcessCmdKey(ref msg, keyData);
61 }
62
63 protected override bool ProcessDialogKey(Keys keyData)
64 {
65 Keys k = keyData & Keys.KeyCode;
66 if (k == Keys.Enter)
67 {
68 return this.ProcessRightKey(keyData );
69 }
70
71 return base.ProcessDialogKey(keyData);
72 }
73
74 protected override bool ProcessDataGridViewKey(KeyEventArgs e)
75 {
76 if (e.KeyCode == Keys.Enter)
77 {
78 return this.ProcessRightKey(e.KeyCode);
79 }
80
81 return base.ProcessDataGridViewKey(e);
82 }
83
84 protected new bool ProcessRightKey(Keys keyData)
85 {
86 Keys k = Keys.KeyCode & keyData;
87 if (k == Keys.Enter)
88 {
89 //如果dgv中输入焦点在最后一行最后一列则新增行,并将输入交代那移到新增行,首列
90 if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex == (base.RowCount - 1)))
91 {
92 if (_operateState == LJFWCommon.ConstEnum.OperateStateEnum.New)
93 {
94 base.Rows.Add(1);
95 }
96 base.CurrentCell = base.Rows[base.RowCount - 1].Cells[""+ _startEditColumnName +""];
97
98 return true;
99 }
100 if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex + 1 != base.NewRowIndex))
101 {
102 base.CurrentCell = base.Rows[base.CurrentCell.RowIndex + 1].Cells[""+ _startEditColumnName +""];
103 return true;
104 }
105 return base.ProcessRightKey(keyData);
106 }
107
108 return base.ProcessRightKey(keyData);
109 }
110 }
(二)DataGridView上移下移当前行
public DataGridView _Dgv = null;
/// <summary>
/// 设置上移下移操作的数据源控件
/// </summary>
public DataGridView Dgv
{
get { return _Dgv; }
set { _Dgv = value; }
}
protected virtual void First()
{
if (_Dgv == null) return;
CurrencyManager cm = this.BindingContext[_Dgv.DataSource] as CurrencyManager;
if (cm != null)
{
cm.Position = 0;
}
}
public virtual void Previous()
{
if (_Dgv == null) return;
CurrencyManager cm = this.BindingContext[_Dgv.DataSource] as CurrencyManager;
if (cm != null)
{
cm.Position--;
}
}
protected virtual void Next()
{
if (_Dgv == null) return;
CurrencyManager cm = this.BindingContext[_Dgv.DataSource] as CurrencyManager;
if (cm != null)
{
cm.Position++;
}
}
protected virtual void Last()
{
if (_Dgv == null) return;
CurrencyManager cm = this.BindingContext[_Dgv.DataSource] as CurrencyManager;
if (cm != null)
{
cm.Position = cm.Count;
}
}