Skip to main content Skip to footer

Undo Feature for Adding/Removing Rows in Spread for Silverlight

Spread for Silverlight provides the functionality where users can undo their last operation using Ctrl+Z. However, one of the Spread users suggested that spread should also offer the option to undo the operation of adding and removing rows. This prompted me to come up with this blog. For the implementation, we need to create a custom class which Implements IUndo interface and inherits from ActionBase class for both our add and remove undo functionality. Following code shows how to Add/Remove Rows :

Public Class AddRowsUndo  
    Inherits ActionBase  
    Implements IUndo  

    Private _row As Integer  
    Private _rowCount As Integer  
    Public Sub New(row As Integer, rowCount As Integer)  
        _row = row  
        _rowCount = rowCount  
    End Sub  

    Public Overrides Function CanExecute(parameter As Object) As Boolean  
        Return True  
    End Function  
    Public Overrides Sub Execute(parameter As Object)  
        Dim sheet = TryCast(parameter, SpreadView).ActiveSheet  
        sheet.AddRows(\_row, \_rowCount)  
    End Sub  
    Public ReadOnly Property CanUndo As Boolean Implements GrapeCity.Windows.SpreadSheet.UI.IUndo.CanUndo  
            Return True  
        End Get  
    End Property  
    Public Sub SaveState() Implements GrapeCity.Windows.SpreadSheet.UI.IUndo.SaveState  

    End Sub  
    Public Function Undo(parameter As Object) As Boolean Implements GrapeCity.Windows.SpreadSheet.UI.IUndo.Undo  
            Dim sheet = TryCast(parameter, SpreadView).ActiveSheet  
            sheet.RemoveRows(\_row, \_rowCount)  
            Return False  
        End Try  
        Return True  
    End Function  

Now we have a custom undo class ready for AddUndo action. Create an object of this AddRowsUndo class and pass it to Do() method of Spread’s UndoManager class.

gcSpreadSheet1.UndoManager.[Do](New AddRowsUndo(2, 3))

With the above code you will be able to add rows and undo it using Ctrl+Z. Please download the attached sample for detailed implementation for Undo action for Add and remove rows. Download VB sample Download C# Sample


comments powered by Disqus