我有一个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;
}
答案 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());
}