Get the new value or index with SelectionChangeCommitted Event of DataGridView Combobox

删除回忆录丶 提交于 2019-12-23 04:21:23

问题


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
    }

回答1:


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.




回答2:


You can get the new Value using:

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



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/8090847/get-the-new-value-or-index-with-selectionchangecommitted-event-of-datagridview-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!