Datagridview comboBox未选择单击/编辑 [英] Datagridview comboBox not selecting on click/edit

查看:88
本文介绍了Datagridview comboBox未选择单击/编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,它的combox列包含两个值。更改行的组合框值后,我将使用该更改更新后端数据库。

I have a datagridview which has a combox column that contains two values. When a row's combobox value has been changed I'm updating the backend database with the change.

核心问题是仅在单击下拉箭头并选择记录后,数据才会更改。如果单击组合框单元格本身并选择该值,则不会执行任何操作,因为组合框选定的项目为空。

The core problem is the data only changes after you click on the drop down arrow, and select the record. If you click on the combobox cell itself and select the value it doesn't do anything because the combobox selected item is empty.

令我感到困惑的是,这在Visual中可以正常工作Studio除了此后的初始单击之外,都可以正常运行,但是当直接运行应用程序时,组合框运行缓慢,需要2到4次组合框单击才能真正检测到值已更改。

What's confusing me is this works correctly within Visual Studio apart from the initial click following this it works fine, but when the application is ran directly the combobox runs slow, it takes 2 to 4 clicks of the combo box to actually detect that the value has changed.

这是editcontrol处理程序。

This is the editcontrol handler.

 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

这是组合框处理程序,数据发生更改并更新数据库

This is the combobox handler where the data change occurs and updates the DB

    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

更新2014年4月4日

我调整了DirtyCell()并删除了if语句,所以它提交了更改命令不管电池是否脏了。

I've tweaked DirtyCell() and removed the if statement, so it commits the change regardless of if the cell is dirty or not. This seems to have made a bit of an improvement.

  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

最初选择组合框仍然存在问题。仍然需要3次以上的单击才能使值更改真正生效并触发combohandler事件。

I've still got an issue with the combobox cell being initially selected. It still requires 3+ clicks for the value change to actually take effect and trigger the combohandler event.

我不知道如何解决此问题。

I am at a loss as to how to resolve this.

推荐答案

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

Instead of trapping and attempting to do an update while the edit event was occurring (hindsight) I used the CellValueChanged event.

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

The Combobox field contains 2 values but by default the value of the cell was empty.

触发CellValueChanged时是在单元格的值已更新之后。

When CellValueChanged is triggered it's after the cell's value has been updated.

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

I'd added CurrentCellDirtyStateChanged to DirtyCell to automatically commit the data change and rerun the query.

像使用CellvalueChanged这样的简单更改即可解决此问题。

Such a simple change as using CellvalueChanged fixed the issue.

这篇关于Datagridview comboBox未选择单击/编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆