我在DataGridViewComboBoxCell中有一个项目列表;我想在用户点击某个值时立即执行某些操作。
什么是事件,或者我怎么能做我想做的事?
当我们离开单元格时调用CellValueChanged ......
答案 0 :(得分:2)
您可以处理组合框的选择变换。
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
// first remove event handler to keep from attaching multiple:
cb.SelectedIndexChanged -= new
EventHandler(cb_SelectedIndexChanged);
// now attach the event handler
cb.SelectedIndexChanged += new
EventHandler(cb_SelectedIndexChanged);
}
}
void cb_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Selected index changed");
}
答案 1 :(得分:0)
另一种可能的方法是:
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb == null)
return;
dataGridView1.EditingControlShowing -= dataGridView1_EditingControlShowing;
cb.SelectedIndexChanged += cb_SelectedIndexChanged;
}
void cb_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Selected index changed");
}
你只做一次。