Articles in this section
Category / Section

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

2 mins read

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

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied