Results 1 to 4 of 4

Thread: [RESOLVED] CancelEdit BindingSource need help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    487

    Resolved [RESOLVED] CancelEdit BindingSource need help

    I have a form with a DataGridView that is bound to a DataTable via a BindingSource. The form also has a button, located below the grid, named btnCancel. This button calls the BindingSource.CancelEdit method.

    When I edit a cell in the grid and before I move to a different row in the grid, I can cancel the pending changes to the row via the ESC key. When the ESC key is clicked, the prior values of all cells in the row are restored to the grid.

    When I click the btnCancel button, this does not happen. The prior values of all cells in the row are not restored to the grid. I suspect that this is because by clicking the button I've left the DataGridView and its BindingSource.EndEdit method is implicitly called. When I inspect the row of the DataTable its RowState is Modified and it does not have a Proposed DataRowVersion.

    If the btnCancel_Click event calls the DataTable.RejectChanges method instead of the BindingSource.CancelEdit method, then all pending changes to all rows/cells are canceled and the prior values are restored to the grid.

    How would I use the btnCancel button to mimic the ESC key to cancel the changes to the current row of the grid?

    Thanks in advance for any suggestions.
    Last edited by Mark@SF; Aug 19th, 2019 at 02:09 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: CancelEdit BindingSource need help

    I haven't tested this and I'm not sure that there isn't another/better way but the first thing that comes to mind is setting the CausesValidation property of that Button to False. By default, the previous control to have focus will be validated when a new control receives focus. If that new control has its CausesValidation property set to False, the previous control will not be validated. This is usually used for Cancel buttons on dialogues, to allow the user to close the form without being prompted to fix data that is being discarded. I think that it should prevent EndEdit being called on your grid, but keep in mind that it will also prevent other controls being validated if the grid was the previously-focused control.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    487

    Re: CancelEdit BindingSource need help

    jmc -

    Many thanks (again) for your response. I have been looking at this issue all day and came across this:

    https://social.msdn.microsoft.com/fo...ndedit-problem

    One of the posts in the above thread said "...because Toolbars do not change focus when they are clicked." This got me to try placing the "Cancel" button in a ToolStrip control on my form. Now clicking the ToolStrip's "Cancel" button does the same thing as clicking the ESC key. Here is the button's Click event code:

    Code:
        Private Sub tsmiCancel_Click(sender As System.Object, e As System.EventArgs) Handles tsmiCancel.Click
    
            '...because ToolStrips do not change focus when they (or one of their controls) are clicked,
            '   the DataGridView control continues to have the focus when you click the tsmiCancel ("cancel") button on the ToolStrip
            '   and therefore the grid's associated BindingSource.EndEdit method will not be implicitly called and
            '   you can then call the grid's BindingSource.CancelEdit method to roll-back (and show) the bound DataRow's prior values
            '       https://social.msdn.microsoft.com/forums/windows/en-us/f9b92a39-2c7d-4c33-b89d-8d58e391eb6a/datagridview-endedit-problem
    
            '...this is not the case when the "cancel" button is not on a ToolStrip
            '   in this case the DataGridView control loses the focus and the associated BindingSource.EndEdit method will be implicitly called
            '   when you click the "cancel" button
            '   once the EndEdit method has been called, the grid row's changes will committed and cannot be rolled-back
    
            Dim strMethodName = New System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name    '...this procedure's name
    
            Dim flgDebug As Boolean = False   '...debug/test purposes only
    
            Dim dr As DataRow
    
            Try
                dr = DirectCast(bsMain.Current, DataRowView).Row
                If flgDebug Then ShowRowInfo(dr, flgDetails:=True)
    
                bsMain.CancelEdit()
                tsmiCancel.Enabled = False
    
                If flgDebug Then ShowRowInfo(dr, flgDetails:=True)
    
            Catch ex As Exception
                HandleError(ex, True)
    
            Finally
                dr = Nothing
    
            End Try
    
        End Sub
    The ShowRowInfo() procedure is used for debugging to iterate through each column of the passed DataRow and write the DataRowVersion values to the Immediate window (Current, Original, and Proposed).

    Here's the form...

    Name:  2019-08-19_20-07-40.jpg
Views: 994
Size:  40.8 KB

    Thanks again for your input and I'm going to mark this thread "Resolved".
    Last edited by Mark@SF; Aug 19th, 2019 at 10:35 PM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    487

    Re: CancelEdit BindingSource need help

    One other thought...I guess that I could also use the original "Cancel" button (shown at the bottom of the above form) to mimic the ESC key if I checked the DataGridView.CurrentRow in the button's Click event to see if the row's RowState is Modified and then iterate through each column in the row to restore the column's Current value to its Original value. I haven't tried that, and I'm not sure that I could programmatically change the row's RowState from Modified to Unchanged after restoring each column's Original value (if its not possible to change the RowState, then the BindingSource would still think it has pending changes).

    Placing the "Cancel" button on the ToolStrip looks like a simpler solution.
    Last edited by Mark@SF; Aug 19th, 2019 at 10:25 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width