如果单元格中的DataGridView.CellValueChanged
编辑控件更改了其选择,我如何强制引发DataGridViewCell.Value
事件(并将更改的实际更改为ComboBox
属性) ?默认情况下,仅在具有ComboBox的单元格失去焦点后才会引发事件。
答案 0 :(得分:6)
我解决了这个问题:
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;
}
答案 1 :(得分:2)
我最终这样做了。我不知道这是否是“首选”的方式,或者它是否会在以后产生任何副作用,但现在似乎有效:
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 :(得分:0)
组合框的值的变化实际上是与网格关联的编辑控件。 因此,要启动任何内容,您必须在DataGrid的EditingControlShowing事件中为该特定列添加这些
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);
}
}
您可以在Combobox的所选cvalue更改事件中调用单元格值更改事件