【问题标题】:What's the best way to get the DropDown event of a ComboBox in a DataGridView在 DataGridView 中获取 ComboBox 的 DropDown 事件的最佳方法是什么
【发布时间】:2009-01-12 18:58:22
【问题描述】:

假设我有一个 DataGridView,我在其中动态构建了一个 ComboBox 列并向其添加值。当用户单击下拉按钮以显示下拉列表时,最好的陷印方法是什么。如果我只是在 DataGridView 上使用 CellClick,即使用户实际上没有点击下拉按钮,我也会收到该事件。

到目前为止,我所做的基本上是继承自 DataGridViewComboBoxCell 并覆盖了 DropDownWidthProperty。在吸气剂中,我触发了一个 DropDown 事件。这有效,但感觉非常“hacky”。还有其他建议吗?

 using System;
    using System.Windows.Forms;

public class DataGridViewEventComboBoxCell : DataGridViewComboBoxCell
{
    private bool dropDownShown = false;
    public event EventHandler BeforeDropDownShown;

    private void OnBeforeDropDownShown()
    {
        if (this.BeforeDropDownShown != null)
        {
            this.BeforeDropDownShown(this, new EventArgs());
        }
    }


    public override int DropDownWidth
    {
        get
        {
            if (!dropDownShown) //this boolean is here because I only need to trap it the very first time
            {
                this.OnBeforeDropDownShown();
                dropDownShown = true;
            }
            return base.DropDownWidth;
        }
        set
        {
            base.DropDownWidth = value;
        }
    }


}

【问题讨论】:

    标签: c# events datagridview


    【解决方案1】:

    试试 EditingControlShowing

    【讨论】:

    • 谢谢谢谢谢谢!!为我节省了大量时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多