Save record to database when moving row in datagrid

Hello,
Ive got a datagrid bound to a datable. The way im saving the data to database is through a 'save button'. But the users want the record to be saved automatically when they  move to another row or they add a new row without them to have to take action.

I was thinking, to check the state of a row everytime they scroll though the rows, and if that row has changes then, save it. BUt im not sure which is the best way to do this.

Any ideas would be helpful!

Thanks!

LVL 8
arcrossAsked:
Who is Participating?
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.

SanclerCommented:
You could use the .CellChanged event of the DataGrid itself to check if the Row has changed and, if it has, then check to see if your DataSet .HasChanges and then, if it has, save the changes.  That would be on these lines

    Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
        Static PreviousRow As Integer = -1
        Dim ThisRow As Integer
        ThisRow = DataGrid1.CurrentCell.RowNumber
        If ThisRow <> PreviousRow Then
            If DataSet.HasChanges Then
                DataAdapter.Update(DataTable)
            End If
        End If
        PreviousRow = ThisRow
    End Sub

Alternatively you could declare a CurrencyManager WithEvents, or add an event handler, and use its .PositionChanged event similarly, but without having to record the previous row's number with this row's number, as the event will only fire when the row changes.  On these lines

    Private Sub cm_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cm.PositionChanged
        If DataSet.HasChanges Then
            DataAdapter.Update(DataTable)
        End If
    End Sub

Roger
0

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
arcrossAuthor Commented:
cheers sancler!
0
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.

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.