I have a DataGridViewComboBoxCell control with some items into it. I would like to get the values when user chooses a value from the dropdown. I cant use DataGridViewComboBoxColumn where EditingControlShowing can be used. I need similar event handler for DataGridViewComboBoxCell. Can anyone help pls.
Please find code sample below:
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;
}
Yes, you can use the EditingControlShowing
event to get a handle to the combobox.
Then hook up an event handler for the SelectedIndexChanged
or whatever event you want and code it..!
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());
}