gpt4 book ai didi

c# - 当用户更改为新值时验证 DataGridViewComboBoxCell

转载 作者:太空宇宙 更新时间:2023-11-03 16:14:19 25 4
gpt4 key购买 nike

我们的 DataGridView 中有一列,用户可以从组合框中为其选择一个值 (DataGridViewComboBoxColumn)。我们有一些用于选择的验证逻辑(覆盖 OnCellValidating)。

烦人的是,用户必须在组合框中进行下拉选择后单击别处,然后才能对该单元格进行验证。我已经尝试在所选索引更改后立即提交编辑(见下文),但在单元格失去焦点之前验证仍然不会触发。我也尝试过使用 EndEdit() 而不是 CommitEdit()

有没有办法让验证在用户选择组合框中的项目时立即触发?

    protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
{
// Validate selection as soon as user clicks combo box item.
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
combo.SelectedIndexChanged -= combo_SelectedIndexChanged;
combo.SelectedIndexChanged += new EventHandler(combo_SelectedIndexChanged);
}

base.OnEditingControlShowing(e);
}

void combo_SelectedIndexChanged(object sender, EventArgs e)
{
this.NotifyCurrentCellDirty(true);
this.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

protected override void OnCellValidating(DataGridViewCellValidatingEventArgs e)
{
// (our validation logic) ...
}

最佳答案

您可以模拟 Tab 键来强制单元格失去焦点:

    private void combo_SelectedIndexChanged(object sender, EventArgs e)
{
//I expect to get the validation to fire as soon as the user
//selects an item in the combo box but the validation
//is not firing until the cell loses focus
//simulate tab key to force the cell to lose focus
SendKeys.Send("{TAB}");
SendKeys.Send("+{TAB}");
}

关于c# - 当用户更改为新值时验证 DataGridViewComboBoxCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16177568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com