)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to undo the covered ranges of the grid in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 29, 2016 |
Last Revised Date: December 16, 2019
Tags: undoredo

Undo Operation

By default, the covered ranges cannot be UNDO from the GridControl. This can be achieved by creating the custom GridModelSetCoveredRangesCommand derived from the GridModelCommand. The covered ranges have to be pushed to the command stack of the GridControl using the Push method of the CommandStack property.

The UNDO operation will reload the covered ranges once CTRL + Z key is pressed after the covered ranges is cleared from the GridControl.

C#

public class GridModelSetCoveredRangesCommand
: GridModelCommand
{
    GridRangeInfoList ranges;
    bool setOrReset;
    private GridModelCoveredRanges cr;
 
    public GridModelSetCoveredRangesCommand(GridModelCoveredRanges cr, GridRangeInfoList ranges, bool setOrReset)
        : base(cr.Model)
    {
        if (setOrReset)
        {
            SetDescription("CommandAddCoveredRanges");
        }
        else
        {
            SetDescription("CommandRemoveCoveredRanges");
        }
 
        this.cr = cr;
        this.ranges = ranges;
        this.setOrReset = setOrReset;
    }
 
    // Executes the command.
    public override void Execute()
    {
        foreach (GridRangeInfo range in ranges)
            cr.Add(range);
        Grid.ScrollCellInView(ranges.ActiveRange, GridScrollCurrentCellReason.Command);
    }
}

 

VB

Public Class GridModelSetCoveredRangesCommand
 Inherits GridModelCommand
 Private ranges As GridRangeInfoList
 Private setOrReset As Boolean
 Private cr As GridModelCoveredRanges
 
 Public Sub New(ByVal cr As GridModelCoveredRanges, ByVal ranges As GridRangeInfoList, ByVal setOrReset As Boolean)
  MyBase.New(cr.Model)
  If setOrReset Then
   SetDescription("CommandAddCoveredRanges")
  Else
   SetDescription("CommandRemoveCoveredRanges")
  End If
 
  Me.cr = cr
  Me.ranges = ranges
  Me.setOrReset = setOrReset
 End Sub
 
 ' Executes the command.
 Public Overrides Sub Execute()
  For Each range As GridRangeInfo In ranges
   cr.Add(range)
  Next range
  Grid.ScrollCellInView(ranges.ActiveRange, GridScrollCurrentCellReason.Command)
 End Sub
End Class

 

Undo and Clear the covered ranges

In the below sample, the covered ranges can be cleared by clicking the ‘Clear covered ranges’ button. And covered ranges can be added to the command stack collection by clicking the ‘Undo the covered ranges’ button. The covered ranges can be restored by pressing CTRL+Z.

C#

//Adding Covered ranges
this.gridControl1.CoveredRanges.Add(GridRangeInfo.Cells(1, 1, 3, 3));
private void button2_Click(object sender, EventArgs e)
{
    GridRangeInfoList list = new GridRangeInfoList();
    list.Add(GridRangeInfo.Cells(1, 1, 3, 3));
    this.gridControl1.CommandStack.Push(new GridModelSetCoveredRangesCommand(this.gridControl1.Model.CoveredRanges, list, false));
}
        
private void button3_Click(object sender, EventArgs e)
{
    this.gridControl1.CoveredRanges.Clear();
}

 

VB

'Adding Covered ranges
Me.gridControl1.CoveredRanges.Add(GridRangeInfo.Cells(1, 1, 3, 3))
private void button2_Click(Object sender, EventArgs e)
 Dim list As New GridRangeInfoList()
 list.Add(GridRangeInfo.Cells(1, 1, 3, 3))
 Me.gridControl1.CommandStack.Push(New GridModelSetCoveredRangesCommand(Me.gridControl1.Model.CoveredRanges, list, False))
 
private void button3_Click(Object sender, EventArgs e)
 Me.gridControl1.CoveredRanges.Clear()

 

Undo the change of the tag of the grid

Undo the change of the tag of the grid

Samples:

C#: Undo/Redo

VB: Undo/Redo

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon