DataGridView中虽然绑定了一个comboBox,但是这个comboBox没有任何事件。
今天想加一个SelectedIndexChanged事件,遍寻网络后找到一可行方法,总结如下:
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
- DataGridViewComboBoxColumn Fparmrangedesc = new DataGridViewComboBoxColumn();
- ……
- ……
- dgView.Columns.Add(Fparmrangedesc);
- dgView.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dgView_EditingControlShowing); //主要是这句
- 写事件方法
- private void dgView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
- {
- if (dgvReportParms.CurrentCell.GetType().Name == "DataGridViewComboBoxCell")
- {
- ((ComboBox)e.Control).SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
- ((ComboBox)e.Control).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
- }
- }
- private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
- {
- //里面想写什么你说了算!
- }
最后,最重要的一步:把DataGridView的CausesValidation属性一定要改为false.(这一步是为什么我还没有搞清楚,希望各位高手帮我解释一下!谢谢了!)