我在 C# winform 应用程序中有一个带有 datagridviewcomboboxcell 的 datagridview。由于 CellValueChanged 事件触发,我可以轻松捕获何时选择新项目。但是,我希望能够检测组合框何时打开,但用户选择已选择的相同值。我怎样才能捕捉到这个?
最佳答案
EditingControlShowing
事件和一些组合框事件的组合有效1。
EditingControlShowing
允许我们访问嵌入的组合框控件:
dataGridView1.EditingControlShowing += new
DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox control = e.Control as ComboBox;
if (control != null)
{
control.DropDown += new EventHandler(control_DropDown);
control.DropDownClosed += new EventHandler(control_DropDownClosed);
}
}
我已在表单中添加了一个私有(private)类级别变量来存储组合框选定的索引。
void control_DropDown(object sender, EventArgs e)
{
ComboBox c = sender as ComboBox;
_currentValue = c.SelectedIndex;
}
void control_DropDownClosed(object sender, EventArgs e)
{
ComboBox c = sender as ComboBox;
if (c.SelectedIndex == _currentValue)
{
MessageBox.Show("no change");
}
}
1. 每次打开和关闭组合框时都会触发此解决方案 - 如果您想要其他内容(例如当组合框提交它对网格的更改时),请更新描述确切内容的问题行为。
关于c# - 检测 DataGridViewComboBoxCell 中同一项目的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10835569/