What event catches a change of value in a combobox in a DataGridViewCell?

寵の児 提交于 2019-11-27 12:45:21

The above answer led me down the primrose path for awhile. It does not work as it causes multiple events to fire and just keeps adding events. The problem is that the above catches the DataGridViewEditingControlShowingEvent and it does not catch the value changed. So it will fire every time you focus then leave the combobox whether it has changed or not.

The last answer about "CurrentCellDirtyStateChanged" is the right way to go. I hope this helps someone avoid going down a rabbit hole.

Here is some code.

        // Add the events to listen for
        dataGridView1.CellValueChanged +=
             new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
        dataGridView1.CurrentCellDirtyStateChanged +=
             new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);

    // This event handler manually raises the CellValueChanged event 
    // by calling the CommitEdit method. 
    void dataGridView1_CurrentCellDirtyStateChanged(object sender,
        EventArgs e)
    {
        if (this.dataGridView1.IsCurrentCellDirty)
        {
            // This fires the cell value changed handler below
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        // My combobox column is the second one so I hard coded a 1, flavor to taste
        DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[1];
        if (cb.Value != null)
        {
               // do stuff
               dataGridView1.Invalidate();
        }
     }

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);
    }

You can also handle the CurrentCellDirtyStateChanged event which gets called whenever a value is changed, even if it's not commited. To get the selected value in the list, you would do something like:

var newValue = dataGridView.CurrentCell.EditedFormattedValue;
Sumeet Hiremath
ComboBox cmbBox = (ComboBox)sender;                
MessageBox.Show(cmbBox.SelectedValue.ToString());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!