VB.NET: 如何avoid/deny DataGridView SelectionChange 事件被处理?
VB.NET: How to avoid/deny DataGridView SelectionChange event from being handled?
这是我的 DataGridView:
Me.DGV_InvoiceContainers.MultiSelect = False
Me.DGV_InvoiceContainers.ReadOnly = True
Me.DGV_InvoiceContainers.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
...
它包含一列和多行。单击 cell/row(与所选的不同)时,将进行逻辑测试(在 MouseDown 事件处理程序下)以确保在更改选择之前满足所有条件。否则,SelectionChanged 事件可能不是 raised/handled,什么也不会发生(就像单击“取消”按钮时一样)。
可能吗?
已解决:
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 ¬ 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
是的,这是可能的。您必须处理 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
这是我的 DataGridView:
Me.DGV_InvoiceContainers.MultiSelect = False
Me.DGV_InvoiceContainers.ReadOnly = True
Me.DGV_InvoiceContainers.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
...
它包含一列和多行。单击 cell/row(与所选的不同)时,将进行逻辑测试(在 MouseDown 事件处理程序下)以确保在更改选择之前满足所有条件。否则,SelectionChanged 事件可能不是 raised/handled,什么也不会发生(就像单击“取消”按钮时一样)。
可能吗?
已解决:
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 ¬ 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
是的,这是可能的。您必须处理 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