编辑单元格中的ComboBox
编辑控件更改其选择后,如何强制引发DataGridView.CellValueChanged
事件(并将其更改为DataGridViewCell.Value
属性)?默认情况下,该事件仅在具有ComboBox的单元失去焦点后才会引发。当组合框中的选择更改时,DataGridView中的提交已更改
4
A
回答
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;
}
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);
}
}
你可以调用在下拉列表中选择cvalue改变事件的单元格的值更改事件
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;
}
相关问题
- 1. 已选择项目更改组合框
- 2. 当组合框选择时从mysql中更改文本框值
- 3. WPF组合框选择更改TabItem选择更改
- 4. 为什么文本更改组合框中选择更改
- 5. 当选择不同的值时,组合框不会更改值
- 6. Xaml当组合框选择变化时更改TextBlock的文本
- 7. 绑定ischecked选择wpf列表框中的复选框已更改组合框
- 8. Silverlight组合框选择已更改或重新选择事件
- 9. 在组合框中选择更改时动态更改列表框项目
- 10. DataGridView ComboBox列选择已更改事件
- 11. 当在DataGridView组合框单元格中发生选择更改时更新相邻单元格的数据源
- 12. 在行更改前提交DataGridView中的更改
- 13. 当复选框值更改时,更改WPF组合框上的数据绑定
- 14. 从DataGridView中的ComboBoxColumn更改选择
- 15. Rails,当用户选择组合框值时更改db值
- 16. Datagridview中的复选框无法更改
- 17. 组合框选定索引更改所有组合框更改
- 18. 提交表单之前,组合框选定的值更改
- 19. 当单击组合框值时,DataGridView列标题更改
- 20. 当选择更改时,自动取消DataForm中的更改
- 21. 在组合框中选择文本时更改标签VB.Net
- 22. 当选择框更改时传递值
- 23. 如何更改组合框中的选择jqgrid的主题
- 24. 更改选择框更改的布局
- 25. 在datagridview中更改组合框在运行时
- 26. 当从组合框更改值时更改价格PHP
- 27. 根据文档中的书签更改组合框选择
- 28. JS jQuery UI的组合框中选择更改事件
- 29. 根据组合框中的选择动态更改html标签
- 30. html选择标记从已选中的复选框更改
这方法不起作用,因为该值尚未提交给底层DataGridViewCell.Value和DataGridView.CellValueChanged的EventHandler,我无法确定新值是什么。我需要找到一种方法来强制datagrid本身将更改提交给单元格并引发`CellValueChanged`事件。 – bitbonk 2011-01-20 08:51:41
在这种情况下,您可以使用SendKeys.Send(“{TAB}”)触发TAB事件;在组合框事件处理程序中强制单元失去焦点,最终触发网格的CellValueChanged – V4Vendetta 2011-01-20 10:32:08
这也不理想,因为当前单元格将失去焦点。这不应该发生在我的情况。 – bitbonk 2011-01-20 13:02:16