DataGridView的组合框柱

DataGridView的组合框柱

本文介绍了DataGridView的组合框柱:从下拉列表中选择后更改单元格的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置为我的DataGridView一个ComboBoxColumn和枚举设置其可选值。这主要是工程,我会与下面的异常喜欢。

I have setup a ComboBoxColumn for my DataGridView and set its selectable values from an enumeration. It mostly works as I would like with the following exception.

每当我点击下拉箭头,然后选择枚举值之一,它仍然是形式的中间状态,则不会触发事件CellValueChanged。我需要关注的另一个单元格或其他控件的事件触发。

Whenever I click the dropdown arrow and then select one of the enum values, it remains in sort of a "intermediate" state where the CellValueChanged event isn't triggered. I need to focus on another cell or another control for the event to fire.

我也有在DataGridView的离开事件的事件处理它通过验证的内容确保无电池是空的。

I also have an event handler for the DataGridView's Leaving event which "validates" the contents by making sure that no cell is empty.

所以,如果我创建一个行并填写所有的细胞,并来到(目前为空白)组合框列,将其更改为一个值,然后点击运行按钮;我的错误对话框弹出,因为组合框的选择不是救。

So, if I create a row and fill all the cells and come to the (currently blank) ComboBox column, change it to a value, and then click a Run button; my error dialog pops up because the ComboBox selection wasn't "saved".

我怎样才能解决这个问题?有没有一种方法,我从下拉菜单中选择一个值后,它会自动设置值?

How can I get around this? Is there a way that after I select a value from the drop down it automatically "sets" the value?

谢谢!

推荐答案

您应该使用 CurrentCellDirtyStateChanged 事件并强制对电网提交编辑:

You should use CurrentCellDirtyStateChanged event and force a commit edit on the grid:

    private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }



希望它帮助!

Hope it helps!

这篇关于DataGridView的组合框柱:从下拉列表中选择后更改单元格的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:38