We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Save record to database when moving row in datagrid

arcross
arcross asked
on
Medium Priority
200 Views
Last Modified: 2010-04-23
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!

Comment
Watch Question

Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
cheers sancler!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.