VB.NET How to Prevent bindingsource row(position) changes during an edit?

In VB.NET, how do I Prevent bindingsource row(position) changes during an edit?  I have a bound datatable (with a bindingsource).  The data is displayed in both:

1) DatagridView (above).  Locked for editing but allows the user to select the row for editing.

2) The current row's fields using bound textboxes (below).    The user edits the information here.

Is there an "edited" or "textchanged" event for a bindingsource that allows me to lock the datagridview to prevent the user from changing the bindingsource position (i.e. datagridview).  I essentially want to for the user to click "SAVE" so I can validate the particular row before the user moves on to another row.  My current plan is to use the multiple textbox's textchanged events, but that's pretty ugly.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
So you have a "split" form, with a list of records on top, and a detail section at the bottom? And your user edits the bottom section (not the datagridview), but you want to prevent the user from moving to a new row if they have edited data, but not Saved it?

If so, you could use a flag to indicate when the user edits a Textbox control in the lower section. In the Declarations section of the form's code window, add this:

Private fDirty As Boolean

In the TextChanged event of your Textboxes, do this:

fDirty = True

Now use the DataGridView's RowValidating event to check that value:

If fDirty Then
  Messagebox.show("Some Message")
  e.Cancel = True
End If

The user couldn't move off that row until you set the value of fDirty to True. You could do that in your Save button's Click event. You may also want to set that value to True in the DataGridView's RowEnter event, just to be sure.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eeyoAuthor Commented:
Perfect.  The RowValidating and RowEnter events are a good idea and allows for an error message. I'll give it a try.  I have been using the TextChanged event and just disabling the entire datagridview with the IsDirty flag.
eeyoAuthor Commented:
I added the Rowvalidating event.  It works fine to display the error message, but then blocks from further editing or saving.  Here is how I reproduce the error:

1) Select a row to edit (e.g. bindingsource.position = 1)
2) Edit a bound textbox, which sets the IsDirty flag to True.
3) Click on a different row in the datagridview
4) The expected Rowvalidating error message displays (in my example, it tells me my position is still at 1)
5) I can no longer continue editing any bound textbox.  They same Rowvalidating error displays.
6) I can't even click my Save buttton (no code is run in the Save button event).  The same Rowvalidating error displays.

Here is the code below.  Did I do something wrong?
    Private Sub AllTextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
        IsDirty = True
    End Sub

    Private Sub dgvSample_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgvSample.RowValidating
        If IsDirty Then
            MessageBox.Show("RowValidating:  Need to Save Changes First, Current Position: " & SampleBindingSource.Position)
            e.Cancel = True
        End If
    End Sub

    Private Sub SampleBindingSource_PositionChanged(sender As Object, e As EventArgs) Handles SampleBindingSource.PositionChanged
        MessageBox.Show("SampleBindingSource_PositionChanged, Current: " & SampleBindingSource.Position)
        IsDirty = False
    End Sub

Open in new window

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You can press the ESC key to "undo" your edits. That should allow you to continue working.

If you find the Validating event to be too cumbersome, you could use other DGV events, like the SelectionChanged event. There are many events in the DGV control, so there are plenty of choices.
eeyoAuthor Commented:
Conceptually works, but still couldn't get it to work with the suggestion.  I'll have to think of something else.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.