VB.NET:如何避免/拒绝处理DataGridView SelectionChange事件?

时间:2015-01-26 02:15:33

标签: vb.net events datagridview handler selectionchanged

这是我的DataGridView:

Me.DGV_InvoiceContainers.MultiSelect = False
Me.DGV_InvoiceContainers.ReadOnly = True
Me.DGV_InvoiceContainers.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
...

它包含一列和多行。单击一个单元格/行(与所选单元格不同)时,将完成逻辑测试(在MouseDown事件处理程序下),以确保在更改选择之前满足所有条件。如果没有,可能不会引发/处理SelectionChanged事件,也不会发生任何事情(比如点击取消按钮时)。

有可能吗?


解决:

Private Sub DGV_InvoiceContainers_RowValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DGV_InvoiceContainers.RowValidating
        If Me.CurrentInvoice.IsOpen Then
            If Me.CurrentInvoice.ChangesHaveBeenMade Then
                Dim dialogResult As New DialogResult
                dialogResult = MessageBoxA.Show("Do you want to save changes?", "Question", _
                                                MessageBoxAAutoFill.Title, _
                                                MessageBoxAButtons.YesNoCancel, Library.MessageBoxAMode.Question, MessageBoxAButtonsAlignment.Right, _
                                                MessageBoxA.DialogResultButtons.Button1, {"&Save", "Do &not save", "&Cancel"})
                Select Case dialogResult
                    Case Windows.Forms.DialogResult.Yes
                        Me.Reader.Close()
                        Me.SaveChanges()
                        Me.InvoiceContainerSelectionAllowed = True
                    Case Windows.Forms.DialogResult.No
                        Me.Reader.Close()
                        Me.InvoiceContainerSelectionAllowed = True
                    Case Else
                        Me.InvoiceContainerSelectionAllowed = False
                End Select
            Else
                Me.Reader.Close()
                Me.InvoiceContainerSelectionAllowed = True
            End If
            '
            If Me.InvoiceContainerSelectionAllowed Then
                Me.DisposeInvoiceData()
            End If
        Else
            Me.InvoiceContainerSelectionAllowed = True
        End If

        If Not Me.InvoiceContainerSelectionAllowed Then
            e.Cancel = True
            Return
        End If
End Sub

1 个答案:

答案 0 :(得分:0)

是的,有可能。您必须处理RowValidating事件并执行逻辑以检查是否应允许用户更改行。如果要取消,请使用eventArgs.Cancel属性并将其设置为true。

Protected Overrides Sub OnRowValidating(e As DataGridViewCellCancelEventArgs)
If DialogResult.No = MessageBox.Show("Select a new record?", "New Record?", MessageBoxButtons.YesNo) Then
    e.Cancel = True
    Return
Else
    MyBase.OnRowValidating(e)
End If
End Sub