使用 DataGridView Combobox 的 SelectionChangeComfilled 事件获取新值或索引

发布于 2024-12-14 19:08:42 字数 608 浏览 9 评论 0原文

我使用 SelectionChangeCommited 来捕获组合框选定索引更改时的事件,但我无法获取它的新值或索引。

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
    }

im using SelectionChangeCommitted to catch the event when a combobox selected index changed, but I can not get it's new value or index.

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
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野鹿林 2024-12-21 19:08:42

您好,尝试使用 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 事件。

Hi try using the CommitEdit keyword (CommitEdit, there is also an example on the MSDN page). Add this to your 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);
    }
}

Then you could just listen for the CellValueChanged and avoid having to try and register for the ComboBoxValueChanged event on the underlying editing control.

反差帅 2024-12-21 19:08:42

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

ComboBox comboBox = sender.Control as ComboBox;
MessageBox.Show(comboBox.Text);

You can get the new Value using:

ComboBox comboBox = sender.Control as ComboBox;
MessageBox.Show(comboBox.Text);
悲喜皆因你 2024-12-21 19:08:42

如果我理解得很好,您正在对组合框中的 SelectionChangeCommissed 事件做出反应,但尝试通过网格获取值。这是正确的吗?

  • RuleList中的承诺是如何完成的?
  • 承诺在那个时间点已经发生了吗?

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

If I understand well, you are reacting on the SelectionChangeCommitted event from a combobox, but trying to get the value via a grid. Is that correct?

  • How is the commitment in the ruleList done?
  • Did the commitment already happen at that point in time?

My feeling here is that via this SelectionChangeCommitted event you can access the value from the combobox directly, but not yet via the grid since it is not commited yet.

浸婚纱 2024-12-21 19:08:42

改进 Killercam 的方法,您可以检查当前单元格是否为 datagridviewcomboboxcell 并执行(在 VB 中,您可以轻松转换为了完整起见,

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

我还添加了 EndEdit() 方法。

Improving on Killercam's method, you can check for the currentcell being a datagridviewcomboboxcell and do (in VB which you can easily convert to C#)

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

I also added the EndEdit() method for completeness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文