如何设置事件Datagridviewcombobox cell selectionchanged?

How to set up the event Datagridviewcombobox cell selectionchanged?

我有一个 DataGridViewComboBoxCell 控件,其中包含一些项目。我想在用户从下拉列表中选择一个值时获取这些值。我不能在可以使用 EditingControlShowing 的地方使用 DataGridViewComboBoxColumn。我需要 DataGridViewComboBoxCell 的类似事件处理程序。任何人都可以帮忙吗?

请在下面找到代码示例:

private DataGridViewComboBoxCell NameDropDown = new DataGridViewComboBoxCell();     

public void SetDropDown(int index)
      {
         NameDropDown = new DataGridViewComboBoxCell();         
         DropDownValues(index);
         for (int j = 0; j < DropDownOld.Items.Count; j++)
         {
            NameDropDown.Items.Add(DropDownOld.Items[j]);
         }
         dataGridView1.Rows[index].Cells[4] = NameDropDown;
      }

是的,您可以使用EditingControlShowing事件来获取组合框的句柄。

然后为 SelectedIndexChanged 或您想要的任何事件连接一个事件处理程序并对其进行编码......!

DataGridViewComboBoxEditingControl cbec = null;

private void dataGridView1_EditingControlShowing(object sender, 
                           DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is DataGridViewComboBoxEditingControl)
    {
        cbec = e.Control as DataGridViewComboBoxEditingControl;
        cbec.SelectedIndexChanged -=Cbec_SelectedIndexChanged;
        cbec.SelectedIndexChanged +=Cbec_SelectedIndexChanged;
    }
}

private void Cbec_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cbec != null) Console.WriteLine(cbec.SelectedItem.ToString());
}