C# DataGridView 中 DataGridViewComboBoxCell 下拉列表框设置选择事件

本文介绍了一个关于DataGridView中使用ComboBox进行编辑时如何添加和移除事件处理程序的方法。通过在DataGridViewEditingControlShowing事件中为DataGridViewComboBoxEditingControl绑定SelectedIndexChanged和Leave事件,实现了对下拉列表框的选择更改和焦点离开的响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    private void DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {

            if (StoreSwap_Tab_DataGridView.CurrentCell != null && e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
            {
                //创建下拉列表框改变事件
                (e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
                //创建下拉列表框离开焦点事件 添加该事件是为了删除动态事件 以免多次调用
                (e.Control as ComboBox).Leave += new EventHandler(ComboBox_Leave);
            }
        }
        //离开焦点事件
    private void ComboBox_Leave(object sender, EventArgs e)
        {
            //删除动态调用
            (sender as ComboBox).SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
            (sender as ComboBox).Leave -= new EventHandler(ComboBox_Leave);
        }
        //下拉列表框改变事件
    private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值