What event catches a change of value in a combobox in a DataGridViewCell?(什么事件会捕获 DataGridViewCell 中组合框中的值变化?)
问题描述
我想在 DataGridView 单元格中的 ComboBox 中更改值时处理该事件.
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.
一个简单的 ComboBox SelectedValueChanged 在选择新值后会立即触发.
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?
推荐答案
上面的回答让我暂时走上了报春花路.它不起作用,因为它会导致多个事件触发并且只是不断添加事件.问题是上面捕获了 DataGridViewEditingControlShowingEvent 并且它没有捕获更改的值.因此,它会在您每次聚焦时触发,然后离开组合框,无论它是否已更改.
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.
关于 CurrentCellDirtyStateChanged 的最后一个答案是正确的方法.我希望这可以帮助人们避免陷入困境.
The last answer about CurrentCellDirtyStateChanged is the right way to go. I hope this helps someone avoid going down a rabbit hole.
这里有一些代码:
// 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();
}
}
这篇关于什么事件会捕获 DataGridViewCell 中组合框中的值变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么事件会捕获 DataGridViewCell 中组合框中的值变化?
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
