C# 检测DataGridViewComboBoxCell中相同项的选择
我在C#winform应用程序中有一个带有DataGridViewComboxCell的datagridview。当选择新项目时,我可以轻松捕获,因为CellValueChanged事件会触发。但是,我希望能够检测何时打开组合框,但用户选择的值与已选择的值相同。如何捕获此信息?尝试使用事件查看: -下拉列表C# 检测DataGridViewComboBoxCell中相同项的选择,c#,winforms,datagridview,datagridviewcomboboxcell,C#,Winforms,Datagridview,Datagridviewcomboboxcell,我在C#winform应用程序中有一个带有DataGridViewComboxCell的datagridview。当选择新项目时,我可以轻松捕获,因为CellValueChanged事件会触发。但是,我希望能够检测何时打开组合框,但用户选择的值与已选择的值相同。如何捕获此信息?尝试使用事件查看: -下拉列表 -DropDownClosed(下拉关闭)显示事件和一些组合框事件的编辑控件的组合1 EditingControlShowing允许我们访问嵌入式组合框控件: dataGridView1.E
-DropDownClosed(下拉关闭)显示事件和一些组合框事件的编辑控件的组合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);
}
}
我在表单中添加了一个私有类级别的变量来存储组合框所选索引
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");
}
}
一,。此解决方案在每次打开和关闭组合框时触发-如果您需要其他内容(例如组合框将其更改提交到网格时),请更新描述确切行为的问题 单元格单击事件如何?据我所知,即使项目/索引根本没有更改,combobox也会触发SelectedIndexChanged事件。您可以将当前选择存储在某个位置,然后将其与用户的选择进行比较。将旧单元格值放入{如果cellvalue为数字,则将其复制到int/short/byte变量中}[如果cellvalue为字符串,则将其复制到标签中]可以在代码中使用的内容,然后将旧单元格值与新单元格值进行比较