什么事件捕获DataGridViewCell中的组合框中的值的变化? [英] What event catches a change of value in a combobox in a DataGridViewCell?

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

问题描述

当在 DataGridView 单元格中的 ComboBox 中更改值时,我想处理该事件。 p>

CellValueChanged 事件,但是一个不会触发,直到我点击 DataGridView



一个简单的 ComboBox SelectedValueChanged 选择新值。



如何将侦听器添加到单元格内的组合框中?

解决方案

上面的答案让我沿着报春花的路径走了一会儿。它不工作,因为它导致多个事件触发,只是继续添加事件。问题是,上述捕获的DataGridViewEditingControlShowingEvent和它不捕获的值改变。所以它会每次你聚焦,然后离开组合框,无论是否改变。



关于CurrentCellDirtyStateChanged的最后一个答案是正确的方式去。



这是一些代码。

  //添加要侦听的事件
dataGridView1.CellValueChanged + =
new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
dataGridView1.CurrentCellDirtyStateChanged + =
new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);

//此事件处理程序通过调用CommitEdit方法手动提升CellValueChanged事件
//。
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if(this.dataGridView1.IsCurrentCellDirty)
{
//改变处理程序
以下dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}

private void dataGridView1_CellValueChanged(object sender,DataGridViewCellEventArgs e)
{
//我的组合框列是第二个,所以我硬编码a 1,味道味道
DataGridViewComboBoxCell cb =(DataGridViewComboBoxCell)dataGridView1.Rows [e.RowIndex] .Cells [1];
if(cb.Value!= null)
{
// do stuff
dataGridView1.Invalidate();
}
}


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

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

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

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

解决方案

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆