Search code examples
c#vb.netdatagridviewcomboboxaddhandler

C# / vb.NET Bound DataGridView: Add Event Handler to DataGridViewComboboxCell


i have a bound dataGridView where i load a list of objects using a bindinglist. Each row has a datagridviewcomboboxCell and a textcell. The comboboxes are filled with enum values. Depending on the item selected in the combobox the textcell should have another cellStyle. E.g. if the user selects the first item of the combobox, the cell should be green, if he selects the second item, the cell should be blue, and so on... I implemented Handlers on the dataGridviewComboboxCell when the user adds a new row to the dataGridView. This works perfect using DataGridView.EditingControlShowing event.

But if i bind a list containing objects to the datagridview, the EditingControlShowing event is not fired. So i need to know how i can add the eventhandlers to the datagridviewComboboxCells when the data is loading into the dataGridView (using datasource).

I figured out, that i can use the DataSourceChanged event getting all data, but i can not access the cells control to add a handler.


Solution

  • As JayV posted: I can use CellFormatting on loading the data. If the user change the item in the combobox, then the handler is added to the combobox and it works as it should.