件捕获的价值在一个DataGridViewCell的组合框的变化

件捕获的价值在一个DataGridViewCell的组合框的变化

本文介绍了什么事件捕获的价值在一个DataGridViewCell的组合框的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要处理一个值时,在组合框被改变了的DataGridView 细胞事件。

I want to handle the event when a value is changed in a ComboBox in a DataGridView cell.

还有的 CellValueChanged 事件,但一个不火,直到我点击别的地方的的DataGridView 里面。

There's the CellValueChanged event, but that one doesn't fire until I click somewhere else inside the DataGridView.

一个简单的组合框 SelectedValueChanged 不火后,立即选择了新的价值。

A simple ComboBox SelectedValueChanged does fire immediately after a new value is selected.

我如何添加一个监听到ComboBox这是细胞内?

How can I add a listener to the combobox that's inside the cell?

推荐答案

这是在code,这将触发选择的事件在DataGridView的组合框:

This is the code, which will fire the event of the selection in the comboBox in the dataGridView:

public Form1()
    {
        InitializeComponent();

        DataGridViewComboBoxColumn cmbcolumn = new DataGridViewComboBoxColumn();
        cmbcolumn.Name = "cmbColumn";
        cmbcolumn.HeaderText = "combobox column";
        cmbcolumn.Items.AddRange(new string[] { "aa", "ac", "aacc" });
        dataGridView1.Columns.Add(cmbcolumn);
        dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
    }

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        ComboBox combo = e.Control as ComboBox;
        if (combo != null)
        {
            combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
            combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
        }
    }

    private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox cb = (ComboBox)sender;
        string item = cb.Text;
        if (item != null)
            MessageBox.Show(item);
    }

这篇关于什么事件捕获的价值在一个DataGridViewCell的组合框的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:38