我有一个datagridview有一个包含两个值的combox列。当一行的combobox值已被更改时,我正在使用更改更新后端数据库。Datagridview组合框没有选择点击/编辑
核心问题是数据只在您点击下拉箭头后才会发生变化,然后选择记录。如果您单击组合框单元格并选择该值,它将不会执行任何操作,因为组合框选定项目为空。
有什么让我感到困惑的是,在Visual Studio中,除了最初的单击之后它能够正常工作,而且它的工作正常,但是当应用程序直接运行时,组合框运行缓慢,需要2到4次组合框才能真正检测价值已经改变。
这是editcontrol处理程序。
Private Sub DataGridView_Changer(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) _
Handles DataGridView.EditingControlShowing
Try
Dim Combo As ComboBox = CType(e.Control, ComboBox)
If Not IsNothing(Combo.SelectedItem) Then
RemoveHandler Combo.SelectedIndexChanged, New EventHandler(AddressOf ComboHandler)
AddHandler Combo.SelectedIndexChanged, New EventHandler(AddressOf ComboHandler)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
这是数据发生变化,并更新数据库的组合框处理
Private Sub ComboHandler(sender As Object, e As EventArgs)
Try
Dim EmailID As Integer = DataGridView.CurrentRow.Cells("EmailID").Value
Dim Sent As Boolean = DataGridView.CurrentRow.Cells("BeenSent").Value
If Sent = True Then
Exit Sub
End If
Dim EMRec As DBClass.EmailRecordDB = EmailArchive.Where(Function(X) X.EmailID = EmailID).FirstOrDefault
Dim Combo As ComboBox = CType(sender, ComboBox)
If Combo.SelectedItem.ToString = "Failed" Then
EMRec.SentAttempt = 999
EMRec.BeenSent = False
ElseIf Combo.SelectedItem.ToString = "Resend" Then
EMRec.SentAttempt = 0
EMRec.BeenSent = False
End If
EMRec.ResetStatus() 'DB Update
DirtyCell()
Exit Sub
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub DirtyCell() 'Handles DataGridView.CurrentCellDirtyStateChanged
If DataGridView.IsCurrentCellDirty Then
Try
DataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit)
ContextualSearch() ' Refresh Grid
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
UPDATE 24/04/2014
我已经调整了DirtyCell(),除去if语句,所以它提交更改,而不管该单元格是否脏。这似乎有所改善。
Sub DirtyCell() 'Handles DataGridView.CurrentCellDirtyStateChanged
Try
DataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit)
ContextualSearch() ' Refresh Grid
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
UPDATE 25/04/2014
我还有具有ComboBox细胞最初被选择的问题。它仍然需要3次点击才能使值更改实际生效并触发combohandler事件。
我不知道如何解决这个问题。
谢谢,我实际上只是尝试使用CellValueChanged事件来执行值更新,并将处理程序恢复到DirtyCell。它似乎更可靠。 – DavidFletcher
我期待我的答案被选中,但我很高兴你通过。我希望你分享这些代码。我最近做了一个项目,在列表框中使用组合框,因为它按我想要的方式工作,对组合框更改操作!该单元比组合本身晚更新。 _regards – Ranhot
我在等我的老板确认它能够满足他的要求,然后我会把它贴出来。 – DavidFletcher