#region 为复合控件添加事件
//声明复合控件的事件
public event DataGridViewCellEventHandler CellClick;
//委托处理的事件代码
protected virtual void OnCellClick(DataGridViewCellEventArgs e)
{
DataGridViewCellEventHandler dg = CellClick;
//如果事件不為空
if (dg != null)
{
dg(dataGV, e);//調用事件
}
}
#endregion
#region 复合函数的构造函数
/// <summary>
/// 构造函数
/// </summary>
public UserDataGridView()
{
InitializeComponent();
//为CellClick事件绑定一个委托事件
dataGV.CellClick += delegate(object sender, DataGridViewCellEventArgs e)
{
OnCellClick(e);
};
}
#endregion
在自定义控件中这样声明以后就可以直接在使用的地方为其写CellClick事件代码了。