修改datagridview的EditMode属性为EdutOnEnter,否则需要点2次以上才出现下拉框
1.给DataGridView添加EditingControlShowing事件;
2.编辑EditingControlShowing事件:
public void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { DataGridView dgv = sender as DataGridView; //判断相应的列 if (dgv.CurrentCell.GetType().Name == "DataGridViewComboBoxCell" && dgv.CurrentCell.RowIndex != -1) { //添加下拉事件 (e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged); } }
3.定义下拉事件
public void ComboBox_SelectedIndexChanged(object sender, EventArgs e) { ComboBox combox=sender as ComboBox; //去掉事件,避免一直触发 combox.Leave+=new EventHandler(combox_Leave); if (combox.SelectedItem != null) { //添加你需要处理的代码 } }
4.添加删除事件
public void combox_Leave(object sender, EventArgs e) { ComboBox combox = sender as ComboBox; //做完处理撤销动态事件 combox.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged); }
来自 https://blog.csdn.net/a312100321/article/details/25195311