Commit changed in DataGridView when selection changed in ComboBox

How can I get the DataGridView.CellValueChanged event to be raised (and did it change the acatally commit to the DataGridViewCell.Value property) as soon as the edit control ComboBox in the cell changes its selection? By default, an event occurs only after a cell with a ComboBox loses focus.

+4
source share
3 answers

I decided to do this:

  myDataGridView.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(myDataGridView_EditingControlShowing); private void myDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl)) { ComboBox cmb = (ComboBox)e.Control; cmb.SelectionChangeCommitted -= new EventHandler(cmb_SelectionChangeCommitted); cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted); } } void cmb_SelectionChangeCommitted(object sender, EventArgs e) { dgvPresupuesto.CurrentCell.Value = ((DataGridViewComboBoxEditingControl)sender).EditingControlFormattedValue; } 
+6
source

I ended it up like this. I have no idea if this is the "preferred" way or if it will give any side effects later, but at the moment it works:

 this.gridView.EditingControlShowing += this.GridViewOnEditingControlShowing; private void GridViewOnEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox cellComboBox = e.Control as ComboBox; if (cellComboBox != null) { // make sure the handler doen't get registered twice cellComboBox.SelectionChangeCommitted -= this.CellComboBoxOnelectionChangeCommitted; cellComboBox.SelectionChangeCommitted += this.CellComboBoxOnelectionChangeCommitted; } } private void CellComboBoxOnelectionChangeCommitted(object sender, EventArgs e) { DataGridViewComboBoxEditingControl comboBox = sender as DataGridViewComboBoxEditingControl; if (sender == null) { return; } if (comboBox.SelectedValue == null) { return; } if (this.gridView.CurrentCell.Value == comboBox.SelectedValue) { return; } this.gridView.CurrentCell.Value = comboBox.SelectedValue; } 
+2
source

Changing the value of combobox is actually an editing element related to the grid. Therefore, to trigger anything, you will need to add them to the EditingControlShowing Event for the DataGrid for this particular column

 private void dg_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (dg.CurrentCell.ColumnIndex == 0) { ComboBox cmbox = e.Control as ComboBox; cmbox.SelectedValueChanged += new EventHandler(cmbox_SelectedValueChanged); } } 

you can trigger an event with a changed cell value in a selected event with a modified salma Combobox

0
source

Source: https://habr.com/ru/post/1336167/


All Articles