0

当组合框选择的索引发生更改时,我使用 SelectionChangeCommitted 来捕获事件,但我无法获得它的新值或索引。

private void ruleList_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is ComboBox)
        {
            ComboBox comboBox = e.Control as ComboBox;
            comboBox.SelectionChangeCommitted += ruleListColumnComboSelectionChanged;
        }
    }

    private void ruleListColumnComboSelectionChanged(object sender, EventArgs e)
    {
        string value = ruleList.CurrentCell.Value.ToString(); // just return the old value before the change
    }
4

4 回答 4

1

您好尝试使用CommitEdit关键字(CommitEdit,在 MSDN 页面上也有一个示例)。将此添加到您的DataGridView

// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
    EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

然后,您可以只收听CellValueChanged并避免必须尝试在基础编辑控件上注册 ComboBoxValueChanged 事件。

于 2011-11-11T08:58:43.897 回答
1

您可以使用以下方法获取新值:

ComboBox comboBox = sender.Control as ComboBox;
MessageBox.Show(comboBox.Text);
于 2011-12-15T21:38:27.187 回答
0

改进Killercam的方法,您可以检查 currentcell 是否为 datagridviewcomboboxcell 并执行(在 VB 中,您可以轻松地将其转换为 C#)

If TypeOf CType(sender, DataGridView).CurrentCell Is DataGridViewComboBoxCell Then
    CType(sender, DataGridView).CommitEdit(DataGridViewDataErrorContexts.Commit)
    CType(sender, DataGridView).EndEdit()
End If

为了完整性,我还添加了EndEdit()方法。

于 2012-03-17T13:18:46.207 回答
0

如果我理解得很好,您是SelectionChangeCommitted从组合框对事件做出反应,但试图通过网格获取值。那是对的吗?

  • ruleList中的commitment是怎么做的?
  • 承诺在那个时间点是否已经发生?

我的感觉是,通过此SelectionChangeCommitted事件,您可以直接从组合框访问值,但还不能通过网格访问,因为它尚未提交。

于 2011-11-11T07:58:50.067 回答