Here is my DataGridView:
Me.DGV_InvoiceContainers.MultiSelect = False
Me.DGV_InvoiceContainers.ReadOnly = True
Me.DGV_InvoiceContainers.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
...
It contains one column and multiple rows. When clicking on a cell/row (different from the selected one), a logic test is done (under MouseDown Event handler) to be sure all conditions are fulfilled before changing selection. If not, SelectionChanged event may not be raised/handled, nothing may happen (Like when clicking on a Cancel button).
Is it possible?
Solved:
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
Yes, it is possible. You have to handle the RowValidating event and do your logic to check if you should allow the user to change row. If you want to cancel, use the eventArgs.Cancel property and set it to 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