I have inherited the DataGridView (WinForms app) and created a custom control using it. The DGV is bound to a BindingSource which uses an object List(of T) for its datasource. The list has validation plumbing that is used to enable/disable an Accept button on the form if the elements are valid (so when the list is valid the accept button turns on and it off when it is valid or not). This happens via the ListChanged event.
What I am attempting to accomplish is this behavior: If the List is valid (the Accept Button is enabled), pressing Enter in the DGV will override its normal behavior (move down one line) and instead fire the Accept button's click event. This is accomplished via the following code:
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = System.Windows.Forms.Keys.Enter Then
' **** THIS IS WHERE A SUGGESTION IS NEEDED to accompllsh this:
' **** If the currentrow is dirty, apply the changes to the datasource
Dim _ctl As System.Windows.Forms.Control = CType(Me.FindForm().AcceptButton, System.Windows.Forms.Control)
If _Ctl IsNot Nothing and also _ctl.Enabled And _ctl.Visible Then
My problem is that pressing Enter on a row which is currently being edited is trapped by the above code BEFORE the changes are committed to the List so the state of the Accept button does not reflect whatever changes have been made to the row (whether modifications or a new row). Therefore Accept may be enabled but the edits would cause it to not be enabled (and should not be Clicked) and vice versa.
Is there a way to determine inside the DGV that the current row is dirty and apply the changes at the point Enter is pressed (and trapped by the ProcessDialogKey override)? Applying the changes to the List would fire the ListChanged event and change the state of the Accept button so the following test would be correct.