在DataGridView中,当我们选择DataGridViewComboBoxColumn时,触发事件。需要EditingControlShowing事件里面写。如下:
////// 显示用于编辑单元格的控件时发生。 /////////private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox cbo = new ComboBox(); if (dataGridView1.CurrentCell.OwningColumn.Name == "DataType" && dataGridView1.CurrentCell.RowIndex != -1) { cbo = e.Control as ComboBox; //保存当前的事件源。为了触发事件后。在取消 cbo.SelectedValueChanged += new EventHandler(cbo_SelectedValueChanged); } } ////// DataGridView中的ComboBox选中事件 /////////void cbo_SelectedValueChanged(object sender, EventArgs e) { ComboBox combox = sender as ComboBox; switch(combox.SelectedValue.ToString()) { default:break; } //做完处理,须撤销动态事件。 combox.SelectedIndexChanged -= new EventHandler(cbo_SelectedValueChanged); }
给DataGridView绑定EditingControlShowing事件即可,代码如下:
dataGridView1.EditingControlShowing += DataGridView1_EditingControlShowing;