0

目前,当使用 ComboBoxes 在 DataGridViewColumn 中选择一个值时,用户必须单击该单元格才能通过CellValueChangedDataGridView 上的事件刷新值。

我想要实现的是,一旦在 ComboBox 中选择了一个值,就会触发刷新。

下面是我尝试做的事情,以便在打开/关闭下拉列表时触发刷新,但它仅在单击组合框并且下拉列表可见时执行,而不是在选择值时执行。

Private Sub PL_DGV_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles PL_DGV.EditingControlShowing
        Dim cb As ComboBox = TryCast(e.Control, ComboBox)
        If cb IsNot Nothing Then
            Dim editingComboBox As ComboBox = DirectCast(e.Control, ComboBox)

            RemoveHandler editingComboBox.SelectedIndexChanged,
                New EventHandler(AddressOf editingComboBox_SelectedIndexChanged)

            AddHandler editingComboBox.SelectedIndexChanged,
                New EventHandler(AddressOf editingComboBox_SelectedIndexChanged)
        End If

End Sub

Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim dgvc As DataGridViewCell = TryCast(Me.PL_DGV.CurrentCell, DataGridViewCell)

    RefreshCarriage(dgvc.RowIndex)

End Sub
4

1 回答 1

0

根据@jmcilhinney 的评论,我设法找到了一个在 C# 中涵盖此内容的线程:Thread here

阅读上述线程后,我的代码:

Private Sub PL_DGV_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles PL_DGV.CurrentCellDirtyStateChanged
            'My DGV Combo Box column is second within the grid therefore manually checking for index 1 column.      
            If PL_DGV.CurrentCell.ColumnIndex = 1 Then
                If PL_DGV.IsCurrentCellDirty Then
                    PL_DGV.CommitEdit(DataGridViewDataErrorContexts.Commit)
                End If
            End If
    End Sub

所以上面触发了CellValueChanged我为 DGV ComboBox 列添加了另一个检查的事件,因为我只需要在编辑该列时运行特定的函数:

Private Sub PL_DGV_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles PL_DGV.CellValueChanged
            If PL_DGV.CurrentCell.ColumnIndex = 1 Then
                    RefreshCarriage(e.RowIndex)
            End If
End Sub

再次感谢@jmcilhinney 的评论并引导我解决问题。

于 2021-07-29T08:19:23.697 回答