2014-04-23 36 views
0

我有一个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事件。

我不知道如何解决这个问题。

回答

0

而不是在编辑事件发生时捕获并尝试进行更新(事后) 我使用了CellValueChanged事件。

Combobox字段包含2个值,但默认情况下单元格的值为空。

当CellValueChanged被触发时,它的单元格的值已被更新。

我已将CurrentCellDirtyStateChanged添加到DirtyCell以自动提交数据更改并重新运行查询。

像使用CellvalueChanged这样简单的修改就解决了这个问题。

1

确定这里是我的想法。 selectedIndexChanged在很多情况下都不能正常工作,比如你的和我的。我更喜欢SelectionChangeCommited。另一个问题是你的AddHandler和removeHandler。您应该将选定的组合存储在全局可变数据库中,然后在开始编辑下一个组合时删除事件处理程序。请参阅下面的建议代码。它是在这里工作我:)

编辑象下面这样DataGridView_Changer:

Private lastCombo As ComboBox ''global variable to remember last combo with eventHandler i 
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 
     If Not IsNothing(lastCombo) Then 
       RemoveHandler lastCombo.SelectionChangeCommitted, New EventHandler(AddressOf ComboHandler) 
     End If 
     lastCombo = Combo 
     AddHandler lastCombo.SelectionChangeCommitted, New EventHandler(AddressOf ComboHandler) 
    End If 
Catch ex As Exception 
    MsgBox(ex.Message) 
End Try 
End Sub 

好运!

+0

谢谢,我实际上只是尝试使用CellValueChanged事件来执行值更新,并将处理程序恢复到DirtyCell。它似乎更可靠。 – DavidFletcher

+0

我期待我的答案被选中,但我很高兴你通过。我希望你分享这些代码。我最近做了一个项目,在列表框中使用组合框,因为它按我想要的方式工作,对组合框更改操作!该单元比组合本身晚更新。 _regards – Ranhot

+0

我在等我的老板确认它能够满足他的要求,然后我会把它贴出来。 – DavidFletcher