Save record to database when moving row in datagrid

Posted on 2006-03-28
Last Modified: 2010-04-23
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!


Question by:arcross
    LVL 34

    Accepted Solution

    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
                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
            End If
        End Sub

    LVL 8

    Author Comment

    cheers sancler!

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now