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?
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.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.