C#winform问题 datagridview中combobox选项改变触发事件用哪个方法
给ComboBox挂事件即可:
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dataGridView = (DataGridView)sender;
if (dataGridView.CurrentCell.GetType().Name == "DataGridViewComboBoxCell")
{
ComboBox comboBox = (ComboBox)e.Control;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
MessageBox.Show(string.Format("选中:{0}", comboBox.Text));
}
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewComboBoxColumn newColumn = new DataGridViewComboBoxColumn();
newColumn.Items.Add("下拉项1");
newColumn.Items.Add("下拉项2");
newColumn.Items.Add("下拉项3");
dataGridView1.Columns.Add(newColumn);
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);