怎么给Datagridview里面的控件添加事件

血刺木木1069
2013-10-09 · TA获得超过3182个赞
知道小有建树答主
回答量:967
采纳率:0%
帮助的人:2283万
展开全部
private DataGridViewComboBoxEditingControl AAADGVComboBox = null; private CalendarEditingControl AAADGVCalendar = null; //添加Datagridview事件EditingControlShowing private void BBBDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e){if (e.Control is DataGridViewComboBoxEditingControl){if (BBBDGV.CurrentCell.OwningColumn.Name == "添加列的名字"){//取得被表示的控件 this.AAADGVComboBox = (DataGridViewComboBoxEditingControl)e.Control; // SelectedIndexChanged事件处理器追加 this.AAADGVComboBox.SelectedIndexChanged += new EventHandler(AAADGVComboBox_SelectedIndexChanged);}}if (e.Control is CalendarEditingControl){if (BBBDGV.CurrentCell.OwningColumn.Name == "列的名字"){this.AAADGVCalendar = (CalendarEditingControl)e.Control; this.AAADGVCalendar.ValueChanged += new EventHandler(AAADGVCalendar_ValueChanged);}}}//CellEndEdit事件处理器 private void BBBDGV_CellEndEdit(object sender, DataGridViewCellEventArgs e){//SelectedIndexChanged事件处理器删除 if (this.AAADGVComboBox != null){this.AAADGVComboBox.SelectedIndexChanged -= new EventHandler(AAADGVComboBox_SelectedIndexChanged); this.AAADGVComboBox = null;}if (this.AAADGVCalendar != null){this.AAADGVCalendar.ValueChanged -= new EventHandler(AAADGVCalendar_ValueChanged); this.AAADGVCalendar = null;}}//在DataGridView中表示的ComboBox的SelectedIndexChanged事件处理器 private void AAADGVComboBox_SelectedIndexChanged(object sender, EventArgs e){//表示被选择的Item DataGridViewComboBoxEditingControl cb = (DataGridViewComboBoxEditingControl)sender; if (cb.SelectedItem.ToString() ){}else if (cb.SelectedItem.ToString() ){}}private void AAADGVCalendar_ValueChanged(object sendar, EventArgs e){try{if (BBBDGV.CurrentCell.OwningColumn.Name == "列名字"){CalendarEditingControl ca = (CalendarEditingControl)sendar; if (ca.Value.ToString() ){}}}catch { }}其中AAA,BBB是datagridview的名字,其中要注册CellEndEdit和EditingControlShowing事件,侦听的具体操作在SelectedIndexChanged和ValueChanged里面实现。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式