Computer
[C#] what event catches a change of value in a combobox in a DataGridViewCell?
창천(蒼天)
2013. 4. 21. 22:15
DataGridView에 combobox cell을 추가한 상태에서 combobox의 값이 변경될 때 발생하는 이벤트 확인법
I want to handle the event when a value is changed in a ComboBox in a DataGridView cell. Theres the CellValueChanged event but that one doesn't fire until i click somewhere else inside the DataGridView. A simple ComboBox SelectedValueChanged does fire immidialy after a new value is selected. How can i add a listener to the combobox thats inside the cell ?
Thank you.
This is the code, which will fire the event of the selection in the comboBox in the dataGridView:
public Form1()
{
InitializeComponent();
DataGridViewComboBoxColumn cmbcolumn = new DataGridViewComboBoxColumn();
cmbcolumn.Name = "cmbColumn";
cmbcolumn.HeaderText = "combobox column";
cmbcolumn.Items.AddRange(new string[] { "aa", "ac", "aacc" });
dataGridView1.Columns.Add(cmbcolumn);
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = (ComboBox)sender;
string item = cb.Text;
if (item != null)
MessageBox.Show(item);
}