How to bind DataGridViewComboBoxColumn to a OnChange event (C#)

Andrej picture Andrej · Jan 28, 2011 · Viewed 12.6k times · Source

I have a standard DataGridView, and my last column is a DataGridViewComboBoxColumn. I would like to add an event so that when the selected index of any of the rows in that column changes, an event is triggered and I save that data to db.

I'm struggling with this for an hour or so and couldn't find any event that would trigger this...

Any help would be appreciated!!!

Answer

JPReddy picture JPReddy · Jan 28, 2011

In the EditingControlShowing event of the DataGridView attach a method to the combobox SelectedIndexChanged event.

For example:

private void DGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
  if (DGV.CurrentCell.ColumnIndex == comboColumnIndex && e.Control is ComboBox)
  {
    ComboBox comboBox = e.Control as ComboBox;
    comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
  }
}

Now in the below method you can do whatever you want:

private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
  // Do saving work here
}