How to create Event Handler for DataGridViewComboBoxColumn in a Data Grid View in Winform

后端 未结 1 1724
渐次进展
渐次进展 2021-01-24 07:44

I have a DataGridViewComboBoxColumn in a data grid view. I have attached a list as a data source . Now i need to fire an event based on the selected index of the combobox..How c

相关标签:
1条回答
  • 2021-01-24 08:25

    Given that the SelectedIndex property belongs to the editing control (which is only active when the DataGridView is in edit mode), you could attach an event handler to EditingControlShowing like so:

    void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
        if (e.Control is ComboBox) {
            // remove handler first to avoid attaching twice
            ((ComboBox)e.Control).SelectedIndexChanged -= MyEventHandler;
            ((ComboBox)e.Control).SelectedIndexChanged += MyEventHandler;
        }
    }
    

    Note that the actual type of the control is DataGridViewComboBoxEditingControl, which extends ComboBox. You only need the functionality from the base class, plus it's less to type.

    0 讨论(0)
提交回复
热议问题