如何解决在 DataGridView.ComboBoxColumn 上触发选定的值更改事件
目前,当使用 ComboBox 在 DataGridViewColumn 中选择一个值时,用户必须单击该单元格才能通过 DataGridView 上的 CellValueChanged
事件刷新这些值。
我想要实现的是,只要在 ComboBox 中选择了一个值,就会触发刷新。
下面是我尝试做的,所以当下拉菜单被打开/关闭时它会触发刷新,但它只在 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 的评论并引导我解决问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。