首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在DataGridViewComboBoxColumn SelectedIndexChanged期间触发的事件

在DataGridViewComboBoxColumn SelectedIndexChanged期间触发的事件
EN

Stack Overflow用户
提问于 2012-06-21 23:46:41
回答 2查看 54.1K关注 0票数 27

我有两列的DataGridView。第一列是TextBoxCol(DataGridViewTextBoxColumn),第二列是ComboBoxCol(DataGridViewComboBoxColumn)

ComboBoxCol更改其选定的索引值时,如何更改TextBoxCol的值?(当所选索引在ComboBoxCol中更改时,应该会发生这种情况。不是在离开专栏之后,就像dataGridView_CellValueChanged一样)

我读了一个主题,几乎有相同的问题,但我不明白答案(根据复选标记应该是正确的)。这是链接。-Almost same topic

EN

Stack Overflow用户

发布于 2014-01-24 07:52:58

这个答案在几个地方流传开来。

使用DataGridViewEditingControlShowingEventHandler将触发比预期更多的事件。在我的测试中,它多次触发事件。

另外,使用combo.SelectedIndexChanged -=事件并不会真正删除事件,它们只是继续堆叠。

不管怎样,我找到了一个似乎可行的解决方案。我包含了下面的代码示例:

代码语言:javascript
运行
复制
// 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 (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();
    }
}
票数 47
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11141872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档