0

コンボボックスで選択されたインデックスが変更されたときにイベントをキャッチするためにSelectionChangeCommittedを使用していますが、新しい値またはインデックスを取得できません。

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

4 に答える 4

1

こんにちは、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 イベントを試行して登録する必要がなくなります。

于 2011-11-11T08:58:43.897 に答える
1

次を使用して新しい値を取得できます。

ComboBox comboBox = sender.Control as ComboBox;
MessageBox.Show(comboBox.Text);
于 2011-12-15T21:38:27.187 に答える
0

私がよく理解していればSelectionChangeCommitted、コンボボックスからイベントに反応していますが、グリッドを介して値を取得しようとしています。あれは正しいですか?

  • ruleList のコミットメントはどのように行われますか?
  • その時点でコミットメントはすでに行われていましたか?

ここでの私の気持ちは、このSelectionChangeCommittedイベントを介してコンボボックスから直接値にアクセスできるが、まだコミットされていないためグリッドを介してアクセスできないということです。

于 2011-11-11T07:58:50.067 に答える
0

Killercamの方法を改善すると、 currentcell が datagridviewcomboboxcellであることを確認して実行できます (VB では、C# に簡単に変換できます)。

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

完全を期すためにEndEdit()メソッドも追加しました。

于 2012-03-17T13:18:46.207 に答える