继承DataGridView

代码
public partial class DataGridViewEx : DataGridView
{
public delegate void ProcessEnterDeletegate();
private event ProcessEnterDeletegate ProcessEnterDeletegateHandler;

public DataGridViewEx()
{
InitializeComponent();
}
//public DataGridViewEx(ProcessEnterDeletegate del)
//{
// InitializeComponent();
// ProcessEnterDeletegateHandler = del;
//}

public void SetHandler(ProcessEnterDeletegate del)
{
ProcessEnterDeletegateHandler
= del;
}

protected override bool ProcessDialogKey(Keys keyData)
{
//是回车键
if (keyData == (System.Windows.Forms.Keys.LButton | System.Windows.Forms.Keys.MButton | System.Windows.Forms.Keys.Back))
{
if (ProcessEnterDeletegateHandler != null) {
ProcessEnterDeletegateHandler();
}
return true;
}
return base.ProcessDialogKey(keyData);
}

}



/////////////// 调用页面///////////////////////

public void SendKey()
{
SendKeys.Send(
"{TAB}");
}

private void Form1_Load(object sender, EventArgs e)
{
this.dataGridViewEx1.SetHandler(SendKey); //绑定处理程序
this.dataGridViewEx1.Focus();
this.f_sjzdTableAdapter.Fill(this.fSZJCM0420DataSet.F_sjzd);
}

//msdn的例子

代码
public class CustomDataGridView : DataGridView
{
[System.Security.Permissions.UIPermission(
System.Security.Permissions.SecurityAction.LinkDemand,
Window
= System.Security.Permissions.UIPermissionWindow.AllWindows)]
protected override bool ProcessDialogKey(Keys keyData)
{
// Extract the key code from the key value.
Keys key = (keyData & Keys.KeyCode);

// Handle the ENTER key as if it were a RIGHT ARROW key.
if (key == Keys.Enter)
{
return this.ProcessRightKey(keyData);
}
return base.ProcessDialogKey(keyData);
}

[System.Security.Permissions.SecurityPermission(
System.Security.Permissions.SecurityAction.LinkDemand, Flags
=
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
// Handle the ENTER key as if it were a RIGHT ARROW key.
if (e.KeyCode == Keys.Enter)
{
return this.ProcessRightKey(e.KeyData);
}
return base.ProcessDataGridViewKey(e);
}
原文地址:https://www.cnblogs.com/wucg/p/1762249.html