How to undo the covered ranges of the grid in WinForms GridControl?
Undo Operation
By default, the covered ranges cannot be UNDO from the WinForms GridControl. This can be achieved by creating a 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.
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);
}
}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 sample below, 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.
//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();
}'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()The screenshots below illustrate how to undo the covered ranges in the Grid


Samples:
C#: Undo/Redo
VB: Undo/Redo