Apply and check the associated DataGridViewComboBoxCell immediately after changing the selection

I have windows forms DataGridViewthat contain some DataGridViewComboBoxCellthat are bound to the original collection using properties DataSource, DisplayMemberand ValueMember. Currently, the combobox cell only makes changes (i.e., DataGridView.CellValueChangedoccurs) after I click on another cell and the combo box loses focus.

How I would ideally make a change immediately after a new value was selected in the drop-down list.

+3
source share
3 answers

DataGridViewComboBoxEditingControl. , . -, , OnSelectedIndexChanged:

protected override void OnSelectedIndexChanged(EventArgs e) {
    base.OnSelectedIndexChanged(e);

    EditingControlValueChanged = true;
    EditingControlDataGridView.NotifyCurrentCellDirty(true);
    EditingControlDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

, DataGridView , .

DataGridViewComboBoxCell EditType, (, return typeof(MyEditingControl);). , .

, CellTemplate DataGridViewComboBoxColumn (, myDataGridViewColumn.CellTemplate = new MyCell();). , .

+1

, , . , - , .

SourceChanged . , , . .

    private void bindingSource_PositionChanged(object sender, EventArgs e)
    {
        (MyBoundType)bindingSource.Current.MyBoundProperty = 
            ((MyChoiceType)comboBindingSource.Current).MyChoiceProperty;
    }
0

, , , - ( VB, VB #, :)

Private _currentCombo As ComboBox

Private Sub grdMain_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles grdMain.EditingControlShowing
    If TypeOf e.Control Is ComboBox Then
        _currentCombo = CType(e.Control, ComboBox)
        AddHandler _currentCombo.SelectedIndexChanged, AddressOf SelectionChangedHandler
    End If
End Sub

Private Sub grdMain_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdMain.CellEndEdit
    If Not _currentCombo Is Nothing Then
        RemoveHandler _currentCombo.SelectedIndexChanged, AddressOf SelectionChangedHandler
        _currentCombo = Nothing
    End If
End Sub

Private Sub SelectionChangedHandler(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim myCombo As ComboBox = CType(sender, ComboBox)
    Dim newInd As Integer = myCombo.SelectedIndex

    //do whatever you want with the new value

    grdMain.NotifyCurrentCellDirty(True)
    grdMain.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub

.

0
source

Source: https://habr.com/ru/post/1771576/


All Articles