Get the new value or index with SelectionChangeCom

2019-06-11 09:54发布

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
    }

4条回答
女痞
2楼-- · 2019-06-11 10:08

You can get the new Value using:

ComboBox comboBox = sender.Control as ComboBox;
MessageBox.Show(comboBox.Text);
查看更多
混吃等死
3楼-- · 2019-06-11 10:14

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.

查看更多
看我几分像从前
4楼-- · 2019-06-11 10:17

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.

查看更多
▲ chillily
5楼-- · 2019-06-11 10:21

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.

查看更多
登录 后发表回答