如何设置事件Datagridviewcombobox cell selectionchanged?

时间:2017-07-31 09:50:33

标签: c# datagridview datagridviewcomboboxcell

我有一个DataGridViewComboBoxCell控件,其中包含一些项目。我想在用户从下拉列表中选择值时获取值。我无法使用DataGridViewComboBoxColumn,可以使用EditingControlShowing。我需要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;
      }

1 个答案:

答案 0 :(得分:1)

是的,您可以使用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());
}