【问题标题】:Detect selection of the same item in a DataGridViewComboBoxCell检测 DataGridViewComboBoxCell 中相同项目的选择
【发布时间】:2012-06-05 19:50:10
【问题描述】:

我在 C# winform 应用程序中有一个带有 datagridviewcomboboxcell 的 datagridview。由于 CellValueChanged 事件触发,我很容易捕捉到何时选择了新项目。但是,我希望能够检测到组合框何时打开,但用户选择了已选择的相同值。我怎样才能捕捉到这个?

【问题讨论】:

  • 单元格点击事件怎么样?
  • 据我所知,即使项目/索引根本没有更改,组合框也会触发 SelectedIndexChanged 事件。您可以将当前选择存储在某处,然后将其与用户的选择进行比较。
  • 将旧单元格值放入 {if cellvalue is number copy into an int/short/byte variable} [if cellvalue is string copy it into a label] 你可以在代码中使用然后比较的东西旧的和新的

标签: c# winforms datagridview datagridviewcomboboxcell


【解决方案1】:

尝试查看事件: - 落下 - 下拉关闭

【讨论】:

    【解决方案2】:

    EditingControlShowing 事件和一些组合框事件的组合起作用1

    EditingControlShowing 允许我们访问嵌入式组合框控件:

    dataGridView1.EditingControlShowing += new 
        DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
    
    
    void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        ComboBox control = e.Control as ComboBox;
    
        if (control != null)
        {            
            control.DropDown += new EventHandler(control_DropDown);
            control.DropDownClosed += new EventHandler(control_DropDownClosed);
        }
    }
    

    我在表单中添加了一个私有类级别变量来存储组合框选定的索引。

    void control_DropDown(object sender, EventArgs e)
    {
        ComboBox c = sender as ComboBox;
    
        _currentValue = c.SelectedIndex;
    }
    
    void control_DropDownClosed(object sender, EventArgs e)
    {
        ComboBox c = sender as ComboBox;
        if (c.SelectedIndex == _currentValue)
        {
            MessageBox.Show("no change");
        }
    }
    

    1. 每次打开和关闭组合框时都会触发此解决方案 - 如果您想要其他内容(例如当组合框提交时,它会更改为网格)更新您描述确切的问题行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多