none
Problem with cancel pending changes - (Bindingnavigator, Datagridview, LINQ to SQL, VB.Net) RRS feed

  • Question

  • Hi everybody,
    I have trouble with implementing an undo button in my binding navigator. I found some entries in this forum, but no a complete solution.
    So, my binding navigator is bound to a datagrid view. I want to cancel with LINQ all pending changes (inserts, deletes & updates) on datagrid view with a undo button.
    I would appreciate any helpful information on this matter.

    Hier is some of my last try in VB.NET

    Me.bindingSrc.CancelEdit()

    Dim changeSet As ChangeSet = dataContext.GetChangeSet
            Dim inserts As New List(Of Object)(changeSet.Inserts)
            Dim deleteAndUpdate As New List(Of Object)(changeSet.Deletes)
            deleteAndUpdate.AddRange(changeSet.Updates)
            Dim iTable As ITable = Nothing
            For Each insert As Object In inserts
                iTable = dataContext.GetTable(insert.GetType)
                iTable.DeleteOnSubmit(insert)
            Next
            dataContext.Refresh(RefreshMode.OverwriteCurrentValues, deleteAndUpdate.ToArray())

    But this code doesn't work properly.

    Any suggestion??

    best regards,
    bayo

    Tuesday, December 15, 2009 11:34 AM

Answers

  • Hello bayo,

    Welcome to LINQ to SQL forum!

    For the deleted entities, I think we also need to use a foreach loop to insert them back to the entities instead of using the Refresh method. 

    ========================================================================
            For Each insert In changeSet.Inserts

                Dim iTable = db.GetTable(insert.GetType)

                iTable.DeleteOnSubmit(insert)

            Next

     

            For Each delete In changeSet.Deletes

                Dim iTable = db.GetTable(delete.GetType)

                iTable.InsertOnSubmit(delete)

            Next

     

            db.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Updates)
    ========================================================================

    Besides, for LINQ to SQL databinding, you may need to refresh the cache inside the DataContext.  I have a deep analysis in this thread, http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/022d72e2-92a1-4645-af1a-59dbcfd85fff.


    Also, I think using a new DataContext could be an easiest way.  :)

    Have a nice weekend!


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, December 19, 2009 11:50 AM
    Moderator

All replies

  • Hello bayo,

    Welcome to LINQ to SQL forum!

    For the deleted entities, I think we also need to use a foreach loop to insert them back to the entities instead of using the Refresh method. 

    ========================================================================
            For Each insert In changeSet.Inserts

                Dim iTable = db.GetTable(insert.GetType)

                iTable.DeleteOnSubmit(insert)

            Next

     

            For Each delete In changeSet.Deletes

                Dim iTable = db.GetTable(delete.GetType)

                iTable.InsertOnSubmit(delete)

            Next

     

            db.Refresh(RefreshMode.OverwriteCurrentValues, changeSet.Updates)
    ========================================================================

    Besides, for LINQ to SQL databinding, you may need to refresh the cache inside the DataContext.  I have a deep analysis in this thread, http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/022d72e2-92a1-4645-af1a-59dbcfd85fff.


    Also, I think using a new DataContext could be an easiest way.  :)

    Have a nice weekend!


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, December 19, 2009 11:50 AM
    Moderator
  • Hi Lingzh,

    Thank you for your help. You detailed description helped me to resolve the problem.

    Best regards,
    Bayardo
    Monday, January 4, 2010 7:27 AM