使用 DataGridView Combobox 的 SelectionChangeComfilled 事件获取新值或索引
我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您好,尝试使用
CommitEdit
关键字 (CommitEdit
,MSDN 页面上也有一个示例)。将其添加到您的DataGridView
中:然后您只需监听
CellValueChanged
并避免必须尝试在底层编辑控件上注册 ComboBoxValueChanged 事件。Hi try using the
CommitEdit
keyword (CommitEdit
, there is also an example on the MSDN page). Add this to yourDataGridView
:Then you could just listen for the
CellValueChanged
and avoid having to try and register for the ComboBoxValueChanged event on the underlying editing control.您可以使用以下方法获取新值:
You can get the new Value using:
如果我理解得很好,您正在对组合框中的
SelectionChangeCommissed
事件做出反应,但尝试通过网格获取值。这是正确的吗?我的感觉是,通过这个
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?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.改进 Killercam 的方法,您可以检查当前单元格是否为 datagridviewcomboboxcell 并执行(在 VB 中,您可以轻松转换为了完整起见,
我还添加了 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#)
I also added the EndEdit() method for completeness.