在 DataGridView.ComboBoxColumn 触发选定值更改事件
Trigger selected value changed event on DataGridView.ComboBoxColumn
目前,当使用组合框在 DataGridViewColumn 中选择一个值时,用户必须单击该单元格才能通过 DataGridView 上的 CellValueChanged
事件刷新值。
我想要实现的是,只要在 ComboBox 中选择了一个值,就会触发刷新。
下面是我尝试做的,所以当下拉菜单是 opened/closed 时它会触发刷新,但它只在单击 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
根据@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 的评论并引导我找到问题的解决方案。
目前,当使用组合框在 DataGridViewColumn 中选择一个值时,用户必须单击该单元格才能通过 DataGridView 上的 CellValueChanged
事件刷新值。
我想要实现的是,只要在 ComboBox 中选择了一个值,就会触发刷新。
下面是我尝试做的,所以当下拉菜单是 opened/closed 时它会触发刷新,但它只在单击 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
根据@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 的评论并引导我找到问题的解决方案。